Repository navigation
#381 - Adds the ability to compile and build executable schemas #386
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
bbakerman
merged 3 commits into
graphql-java:master
from
bbakerman:381-graphql-schema-idl-support
Apr 30, 2017
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
de340f0
#381 - Adds the ability to compile and build executable schemas from …
bbakerman ecde98e
#381 - Added more tests for merging and change the signature to throw…
bbakerman bd5254f
PR feedback - moved classes around and made the IDL spec support call
bbakerman 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
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,125 @@ | ||
| package graphql.schema.idl; | ||
|
|
||
| import graphql.schema.BuilderFunction; | ||
| import graphql.schema.DataFetcher; | ||
| import graphql.schema.GraphQLScalarType; | ||
| import graphql.schema.GraphQLSchema; | ||
| import graphql.schema.TypeResolver; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A runtime wiring is a specification of data fetchers, type resolves and custom scalars that are needed | ||
| * to wire together a functional {@link GraphQLSchema} | ||
| */ | ||
| public class RuntimeWiring { | ||
|
|
||
| private final Map<String, Map<String, DataFetcher>> dataFetchers; | ||
| private final Map<String, GraphQLScalarType> scalars; | ||
| private final Map<String, TypeResolver> typeResolvers; | ||
|
|
||
| private RuntimeWiring(Map<String, Map<String, DataFetcher>> dataFetchers, Map<String, GraphQLScalarType> scalars, Map<String, TypeResolver> typeResolvers) { | ||
| this.dataFetchers = dataFetchers; | ||
| this.scalars = scalars; | ||
| this.typeResolvers = typeResolvers; | ||
| } | ||
|
|
||
| public Map<String, GraphQLScalarType> getScalars() { | ||
| return new LinkedHashMap<>(scalars); | ||
| } | ||
|
|
||
| public Map<String, Map<String, DataFetcher>> getDataFetchers() { | ||
| return dataFetchers; | ||
| } | ||
|
|
||
| public Map<String, DataFetcher> getDataFetcherForType(String typeName) { | ||
| return dataFetchers.computeIfAbsent(typeName, k -> new LinkedHashMap<>()); | ||
| } | ||
|
|
||
| public Map<String, TypeResolver> getTypeResolvers() { | ||
| return typeResolvers; | ||
| } | ||
|
|
||
| /** | ||
| * @return a builder of Runtime Wiring | ||
| */ | ||
| public static Builder newRuntimeWiring() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private final Map<String, Map<String, DataFetcher>> dataFetchers = new LinkedHashMap<>(); | ||
| private final Map<String, GraphQLScalarType> scalars = new LinkedHashMap<>(); | ||
| private final Map<String, TypeResolver> typeResolvers = new LinkedHashMap<>(); | ||
|
|
||
| private Builder() { | ||
| ScalarInfo.STANDARD_SCALARS.forEach(this::scalar); | ||
| } | ||
|
|
||
| /** | ||
| * This allows you to add in new custom Scalar implementations beyond the standard set. | ||
| * | ||
| * @param scalarType the new scalar implementation | ||
| * | ||
| * @return the runtime wiring builder | ||
| */ | ||
| public Builder scalar(GraphQLScalarType scalarType) { | ||
| scalars.put(scalarType.getName(), scalarType); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * This allows you to add a new type wiring via a builder | ||
| * | ||
| * @param builder the type wiring builder to use | ||
| * | ||
| * @return this outer builder | ||
| */ | ||
| public Builder type(TypeRuntimeWiring.Builder builder) { | ||
| return type(builder.build()); | ||
| } | ||
|
|
||
| /** | ||
| * This form allows a lambda to be used as the builder of a type wiring | ||
| * | ||
| * @param builderFunction a function that will be given the builder to use | ||
| * | ||
| * @return the runtime wiring builder | ||
| */ | ||
| public Builder type(BuilderFunction<TypeRuntimeWiring.Builder> builderFunction) { | ||
| TypeRuntimeWiring.Builder builder = builderFunction.apply(TypeRuntimeWiring.newTypeWiring()); | ||
| return type(builder.build()); | ||
| } | ||
|
|
||
| /** | ||
| * This adds a type wiring | ||
| * | ||
| * @param typeRuntimeWiring the new type wiring | ||
| * | ||
| * @return the runtime wiring builder | ||
| */ | ||
| public Builder type(TypeRuntimeWiring typeRuntimeWiring) { | ||
| String typeName = typeRuntimeWiring.getTypeName(); | ||
| Map<String, DataFetcher> typeDataFetchers = dataFetchers.computeIfAbsent(typeName, k -> new LinkedHashMap<>()); | ||
| typeRuntimeWiring.getFieldDataFetchers().forEach(typeDataFetchers::put); | ||
|
|
||
| TypeResolver typeResolver = typeRuntimeWiring.getTypeResolver(); | ||
| if (typeResolver != null) { | ||
| this.typeResolvers.put(typeName, typeResolver); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @return the built runtime wiring | ||
| */ | ||
| public RuntimeWiring build() { | ||
| return new RuntimeWiring(dataFetchers, scalars, typeResolvers); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
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,70 @@ | ||
| package graphql.schema.idl; | ||
|
|
||
| import graphql.Scalars; | ||
| import graphql.language.ScalarTypeDefinition; | ||
| import graphql.schema.GraphQLScalarType; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Info on all the standard scalar objects provided by graphql-java | ||
| */ | ||
| public class ScalarInfo { | ||
| /** | ||
| * A list of the scalar types provided by graphql | ||
| */ | ||
| public static final List<GraphQLScalarType> STANDARD_SCALARS = new ArrayList<>(); | ||
|
|
||
| /** | ||
| * A map of scalar type definitions provided by graphql | ||
| */ | ||
| public static final Map<String, ScalarTypeDefinition> STANDARD_SCALAR_DEFINITIONS = new LinkedHashMap<>(); | ||
|
|
||
| static { | ||
| STANDARD_SCALARS.add(Scalars.GraphQLInt); | ||
| STANDARD_SCALARS.add(Scalars.GraphQLFloat); | ||
| STANDARD_SCALARS.add(Scalars.GraphQLString); | ||
| STANDARD_SCALARS.add(Scalars.GraphQLBoolean); | ||
| STANDARD_SCALARS.add(Scalars.GraphQLID); | ||
|
|
||
| STANDARD_SCALARS.add(Scalars.GraphQLBigDecimal); | ||
| STANDARD_SCALARS.add(Scalars.GraphQLBigInteger); | ||
| STANDARD_SCALARS.add(Scalars.GraphQLByte); | ||
| STANDARD_SCALARS.add(Scalars.GraphQLChar); | ||
| STANDARD_SCALARS.add(Scalars.GraphQLShort); | ||
| STANDARD_SCALARS.add(Scalars.GraphQLLong); | ||
| } | ||
|
|
||
| static { | ||
| // graphql standard scalars | ||
| STANDARD_SCALAR_DEFINITIONS.put("Int", new ScalarTypeDefinition("Int")); | ||
| STANDARD_SCALAR_DEFINITIONS.put("Float", new ScalarTypeDefinition("Float")); | ||
| STANDARD_SCALAR_DEFINITIONS.put("String", new ScalarTypeDefinition("String")); | ||
| STANDARD_SCALAR_DEFINITIONS.put("Boolean", new ScalarTypeDefinition("Boolean")); | ||
| STANDARD_SCALAR_DEFINITIONS.put("ID", new ScalarTypeDefinition("ID")); | ||
|
|
||
| // graphql-java library extensions | ||
| STANDARD_SCALAR_DEFINITIONS.put("Long", new ScalarTypeDefinition("Long")); | ||
| STANDARD_SCALAR_DEFINITIONS.put("BigInteger", new ScalarTypeDefinition("BigInteger")); | ||
| STANDARD_SCALAR_DEFINITIONS.put("BigDecimal", new ScalarTypeDefinition("BigDecimal")); | ||
| STANDARD_SCALAR_DEFINITIONS.put("Short", new ScalarTypeDefinition("Short")); | ||
| STANDARD_SCALAR_DEFINITIONS.put("Char", new ScalarTypeDefinition("Char")); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Returns true if the scalar type is a standard one provided by graphql | ||
| * | ||
| * @param scalarType the type in question | ||
| * | ||
| * @return true if the scalar type is a standard one provided by graphql | ||
| */ | ||
| public static boolean isStandardScalar(GraphQLScalarType scalarType) { | ||
| return STANDARD_SCALARS.stream().anyMatch(sc -> sc.getName().equals(scalarType.getName())); | ||
| } | ||
|
|
||
| } |
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.
I think maybe the name RuntimeWiring could be a bit misleading, why not call it ResolversMap like in graphql-tools?
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.
I thought about calling it "Resolvers" but decided against it because DataFetchers are not known as Resolvers and hence it doesnt ring true for graphql-java
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.
We could rename DataFetcher to Resolver.