-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Introspection parser #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Introspection parser #463
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9883c82
wip
andimarek e74167a
wip
andimarek a0de08e
wip
andimarek d677729
enums
andimarek 1ab4eaa
wip
andimarek cb030f7
wip
andimarek c9df114
wip
andimarek eae0efa
wip
andimarek eea8ef7
wip
andimarek ab6405c
done for now
andimarek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
232 changes: 232 additions & 0 deletions
232
src/main/java/graphql/introspection/IntrospectionResultToSchema.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| package graphql.introspection; | ||
|
|
||
| import graphql.PublicApi; | ||
| import graphql.language.Comment; | ||
| import graphql.language.Document; | ||
| import graphql.language.EnumTypeDefinition; | ||
| import graphql.language.EnumValueDefinition; | ||
| import graphql.language.FieldDefinition; | ||
| import graphql.language.InputObjectTypeDefinition; | ||
| import graphql.language.InputValueDefinition; | ||
| import graphql.language.InterfaceTypeDefinition; | ||
| import graphql.language.ListType; | ||
| import graphql.language.NonNullType; | ||
| import graphql.language.ObjectTypeDefinition; | ||
| import graphql.language.OperationTypeDefinition; | ||
| import graphql.language.SchemaDefinition; | ||
| import graphql.language.SourceLocation; | ||
| import graphql.language.StringValue; | ||
| import graphql.language.Type; | ||
| import graphql.language.TypeDefinition; | ||
| import graphql.language.TypeName; | ||
| import graphql.language.UnionTypeDefinition; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static graphql.Assert.assertNotNull; | ||
| import static graphql.Assert.assertShouldNeverHappen; | ||
| import static graphql.Assert.assertTrue; | ||
|
|
||
| @PublicApi | ||
| public class IntrospectionResultToSchema { | ||
|
|
||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public Document createSchemaDefinition(Map<String, Object> introspectionResult) { | ||
| assertTrue(introspectionResult.get("__schema") != null, "__schema expected"); | ||
| Map<String, Object> schema = (Map<String, Object>) introspectionResult.get("__schema"); | ||
|
|
||
| SchemaDefinition schemaDefinition = new SchemaDefinition(); | ||
|
|
||
| Map<String, Object> queryType = (Map<String, Object>) schema.get("queryType"); | ||
| assertNotNull(queryType, "queryType expected"); | ||
| TypeName query = new TypeName((String) queryType.get("name")); | ||
| schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("query", query)); | ||
|
|
||
| Map<String, Object> mutationType = (Map<String, Object>) schema.get("mutationType"); | ||
| if (mutationType != null) { | ||
| TypeName mutation = new TypeName((String) mutationType.get("name")); | ||
| schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("mutation", mutation)); | ||
| } | ||
|
|
||
| Map<String, Object> subscriptionType = (Map<String, Object>) schema.get("subscriptionType"); | ||
| if (subscriptionType != null) { | ||
| TypeName subscription = new TypeName((String) subscriptionType.get("name")); | ||
| schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("subscription", subscription)); | ||
| } | ||
|
|
||
| Document document = new Document(); | ||
| document.getDefinitions().add(schemaDefinition); | ||
|
|
||
| List<Map<String, Object>> types = (List<Map<String, Object>>) schema.get("types"); | ||
| for (Map<String, Object> type : types) { | ||
| TypeDefinition typeDefinition = createTypeDefinition(type); | ||
| if (typeDefinition == null) continue; | ||
| document.getDefinitions().add(typeDefinition); | ||
| } | ||
|
|
||
| return document; | ||
| } | ||
|
|
||
| private TypeDefinition createTypeDefinition(Map<String, Object> type) { | ||
| String kind = (String) type.get("kind"); | ||
| String name = (String) type.get("name"); | ||
| if (name.startsWith("__")) return null; | ||
| switch (kind) { | ||
| case "INTERFACE": | ||
| return createInterface(type); | ||
| case "OBJECT": | ||
| return createObject(type); | ||
| case "UNION": | ||
| return createUnion(type); | ||
| case "ENUM": | ||
| return createEnum(type); | ||
| case "INPUT_OBJECT": | ||
| return createInputObject(type); | ||
| case "SCALAR": | ||
| // todo don't ignore all scalars | ||
| return null; | ||
| default: | ||
| return assertShouldNeverHappen("unexpected kind " + kind); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @SuppressWarnings("unchecked") | ||
| UnionTypeDefinition createUnion(Map<String, Object> input) { | ||
| assertTrue(input.get("kind").equals("UNION"), "wrong input"); | ||
|
|
||
| UnionTypeDefinition unionTypeDefinition = new UnionTypeDefinition((String) input.get("name")); | ||
| unionTypeDefinition.setComments(toComment((String) input.get("description"))); | ||
|
|
||
| List<Map<String, Object>> possibleTypes = (List<Map<String, Object>>) input.get("possibleTypes"); | ||
|
|
||
| for (Map<String, Object> possibleType : possibleTypes) { | ||
| TypeName typeName = new TypeName((String) possibleType.get("name")); | ||
| unionTypeDefinition.getMemberTypes().add(typeName); | ||
| } | ||
|
|
||
| return unionTypeDefinition; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| EnumTypeDefinition createEnum(Map<String, Object> input) { | ||
| assertTrue(input.get("kind").equals("ENUM"), "wrong input"); | ||
|
|
||
| EnumTypeDefinition enumTypeDefinition = new EnumTypeDefinition((String) input.get("name")); | ||
| enumTypeDefinition.setComments(toComment((String) input.get("description"))); | ||
|
|
||
| List<Map<String, Object>> enumValues = (List<Map<String, Object>>) input.get("enumValues"); | ||
|
|
||
| for (Map<String, Object> enumValue : enumValues) { | ||
|
|
||
| EnumValueDefinition enumValueDefinition = new EnumValueDefinition((String) enumValue.get("name")); | ||
| enumValueDefinition.setComments(toComment((String) enumValue.get("description"))); | ||
| enumTypeDefinition.getEnumValueDefinitions().add(enumValueDefinition); | ||
| } | ||
|
|
||
| return enumTypeDefinition; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| InterfaceTypeDefinition createInterface(Map<String, Object> input) { | ||
| assertTrue(input.get("kind").equals("INTERFACE"), "wrong input"); | ||
|
|
||
| InterfaceTypeDefinition interfaceTypeDefinition = new InterfaceTypeDefinition((String) input.get("name")); | ||
| interfaceTypeDefinition.setComments(toComment((String) input.get("description"))); | ||
| List<Map<String, Object>> fields = (List<Map<String, Object>>) input.get("fields"); | ||
| interfaceTypeDefinition.getFieldDefinitions().addAll(createFields(fields)); | ||
|
|
||
| return interfaceTypeDefinition; | ||
|
|
||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| InputObjectTypeDefinition createInputObject(Map<String, Object> input) { | ||
| assertTrue(input.get("kind").equals("INPUT_OBJECT"), "wrong input"); | ||
|
|
||
| InputObjectTypeDefinition inputObjectTypeDefinition = new InputObjectTypeDefinition((String) input.get("name")); | ||
| inputObjectTypeDefinition.setComments(toComment((String) input.get("description"))); | ||
| List<Map<String, Object>> fields = (List<Map<String, Object>>) input.get("inputFields"); | ||
| List<InputValueDefinition> inputValueDefinitions = createInputValueDefinitions(fields); | ||
| inputObjectTypeDefinition.getInputValueDefinitions().addAll(inputValueDefinitions); | ||
|
|
||
| return inputObjectTypeDefinition; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| ObjectTypeDefinition createObject(Map<String, Object> input) { | ||
| assertTrue(input.get("kind").equals("OBJECT"), "wrong input"); | ||
|
|
||
| ObjectTypeDefinition objectTypeDefinition = new ObjectTypeDefinition((String) input.get("name")); | ||
| objectTypeDefinition.setComments(toComment((String) input.get("description"))); | ||
| List<Map<String, Object>> fields = (List<Map<String, Object>>) input.get("fields"); | ||
|
|
||
| objectTypeDefinition.getFieldDefinitions().addAll(createFields(fields)); | ||
|
|
||
| return objectTypeDefinition; | ||
| } | ||
|
|
||
| private List<FieldDefinition> createFields(List<Map<String, Object>> fields) { | ||
| List<FieldDefinition> result = new ArrayList<>(); | ||
| for (Map<String, Object> field : fields) { | ||
| FieldDefinition fieldDefinition = new FieldDefinition((String) field.get("name")); | ||
| fieldDefinition.setComments(toComment((String) field.get("description"))); | ||
| fieldDefinition.setType(createTypeIndirection((Map<String, Object>) field.get("type"))); | ||
|
|
||
| List<Map<String, Object>> args = (List<Map<String, Object>>) field.get("args"); | ||
| List<InputValueDefinition> inputValueDefinitions = createInputValueDefinitions(args); | ||
| fieldDefinition.getInputValueDefinitions().addAll(inputValueDefinitions); | ||
| result.add(fieldDefinition); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private List<InputValueDefinition> createInputValueDefinitions(List<Map<String, Object>> args) { | ||
| List<InputValueDefinition> result = new ArrayList<>(); | ||
| for (Map<String, Object> arg : args) { | ||
| Type argType = createTypeIndirection((Map<String, Object>) arg.get("type")); | ||
| InputValueDefinition inputValueDefinition = new InputValueDefinition((String) arg.get("name"), argType); | ||
| inputValueDefinition.setComments(toComment((String) arg.get("description"))); | ||
|
|
||
| if (arg.get("defaultValue") != null) { | ||
| StringValue defaultValue = new StringValue((String) arg.get("defaultValue")); | ||
| inputValueDefinition.setDefaultValue(defaultValue); | ||
| } | ||
| result.add(inputValueDefinition); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private Type createTypeIndirection(Map<String, Object> type) { | ||
| String kind = (String) type.get("kind"); | ||
| switch (kind) { | ||
| case "INTERFACE": | ||
| case "OBJECT": | ||
| case "UNION": | ||
| case "ENUM": | ||
| case "INPUT_OBJECT": | ||
| case "SCALAR": | ||
| return new TypeName((String) type.get("name")); | ||
| case "NON_NULL": | ||
| return new NonNullType(createTypeIndirection((Map<String, Object>) type.get("ofType"))); | ||
| case "LIST": | ||
| return new ListType(createTypeIndirection((Map<String, Object>) type.get("ofType"))); | ||
| default: | ||
| return assertShouldNeverHappen("Unknown kind " + kind); | ||
| } | ||
| } | ||
|
|
||
| private List<Comment> toComment(String description) { | ||
| if (description == null) return Collections.emptyList(); | ||
| Comment comment = new Comment(description, new SourceLocation(1, 1)); | ||
| return Arrays.asList(comment); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice one