Skip to content

Commit

Permalink
feat: predefined scalars
Browse files Browse the repository at this point in the history
Co-authored-by: Ovidiu Popoviciu <devovip13@gmail.com>
  • Loading branch information
lgandecki and ovidiup13 committed Jun 7, 2021
1 parent 6ffee23 commit 37160e8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/generate/parse-graphql/getScalars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,31 @@ test('ignore built-in Upload scalar', () => {

expect(res).toEqual(['First', 'ThirdOne']);
});

test('ignore predefined scalars', () => {
const schemaString = gql`
type TodoItem @key(fields: "id") {
id: ID!
list: List
}
extend type List {
id: ID!
todos: [TodoItem!]!
incompleteCount: Int!
}
type InMemory {
id: ID!
}
scalar First
scalar Predefined @predefined
scalar Upload
scalar ThirdOne
`;

const res = getScalars(schemaString);

expect(res).toEqual(['First', 'ThirdOne']);
});
19 changes: 12 additions & 7 deletions src/generate/parse-graphql/getScalars.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import gql from 'graphql-tag';
import { ScalarTypeDefinitionNode } from 'graphql/language/ast';

export default (graphqlString: string): string[] => {
const graphqlAST = gql`
${graphqlString}
`;

return (
graphqlAST.definitions
.filter((d) => d.kind === 'ScalarTypeDefinition')
// @ts-ignore
.map((f) => f.name.value)
.filter((s) => s !== 'Upload')
);
return (graphqlAST.definitions.filter((d) => d.kind === 'ScalarTypeDefinition') as ScalarTypeDefinitionNode[])
.filter((d) => {
const isPredefined = Boolean(
d.directives?.find((directive) => {
return directive.name.value === 'predefined';
}),
);
return !isPredefined;
})
.map((f) => f.name.value)
.filter((s) => s !== 'Upload');
};
2 changes: 2 additions & 0 deletions src/generate/templates/genericDataModelSchema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ directive @requires(fields: _FieldSet!) on FIELD_DEFINITION
directive @provides(fields: _FieldSet!) on FIELD_DEFINITION
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
directive @extends on OBJECT

directive @predefined on SCALAR

0 comments on commit 37160e8

Please sign in to comment.