Skip to content

Commit da07e9b

Browse files
committed
Merge remote-tracking branch 'upstream/master' into 379-mutation-documentation
# Conflicts: # src/test/groovy/readme/ReadmeExamples.java
2 parents 5da912f + a911955 commit da07e9b

58 files changed

Lines changed: 4797 additions & 56 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
.idea
33
.gradle
44
build
5+
classes
56
_site
67
generated-src/

‎README.md‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,117 @@ public Object executeOperation(@RequestBody Map body) {
518518
}
519519
```
520520
521+
### Schema IDL support
522+
523+
This library allows for "schema driven" development of graphql applications.
524+
525+
It allows you to compile a set of schema files into a executable `GraphqlSchema`.
526+
527+
528+
So given a graphql schema input file like :
529+
530+
```graphql
531+
532+
schema {
533+
query: QueryType
534+
}
535+
536+
type QueryType {
537+
hero(episode: Episode): Character
538+
human(id : String) : Human
539+
droid(id: ID!): Droid
540+
}
541+
542+
543+
enum Episode {
544+
NEWHOPE
545+
EMPIRE
546+
JEDI
547+
}
548+
549+
interface Character {
550+
id: ID!
551+
name: String!
552+
friends: [Character]
553+
appearsIn: [Episode]!
554+
}
555+
556+
type Human implements Character {
557+
id: ID!
558+
name: String!
559+
friends: [Character]
560+
appearsIn: [Episode]!
561+
homePlanet: String
562+
}
563+
564+
type Droid implements Character {
565+
id: ID!
566+
name: String!
567+
friends: [Character]
568+
appearsIn: [Episode]!
569+
primaryFunction: String
570+
}
571+
572+
573+
```
574+
575+
You could compile and generate an executable schema via
576+
577+
```java
578+
SchemaCompiler schemaCompiler = new SchemaCompiler();
579+
SchemaGenerator schemaGenerator = new SchemaGenerator();
580+
581+
File schemaFile = loadSchema("starWarsSchema.graphqls");
582+
583+
TypeDefinitionRegistry typeRegistry = schemaCompiler.compile(schemaFile);
584+
RuntimeWiring wiring = buildRuntimeWiring();
585+
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, wiring);
586+
587+
```
588+
589+
The static schema definition file has the field and type definitions but you need a runtime wiring to make
590+
it a truly executable schema.
591+
592+
The runtime wiring contains `DataFetchers`, `TypeResolvers` and custom `Scalars` that are needed to make a fully
593+
executable schema.
594+
595+
You wire this together using this builder pattern
596+
597+
```java
598+
599+
RuntimeWiring buildRuntimeWiring() {
600+
return RuntimeWiring.newRuntimeWiring()
601+
.scalar(CustomScalar)
602+
// this uses builder function lambda syntax
603+
.type(typeWiring -> typeWiring.typeName("QueryType")
604+
.dataFetcher("hero", new StaticDataFetcher(StarWarsData.getArtoo()))
605+
.dataFetcher("human", StarWarsData.getHumanDataFetcher())
606+
.dataFetcher("droid", StarWarsData.getDroidDataFetcher())
607+
)
608+
.type(typeWiring -> typeWiring.typeName("Human")
609+
.dataFetcher("friends", StarWarsData.getFriendsDataFetcher())
610+
)
611+
// you can use builder syntax if you don't like the lambda syntax
612+
.type(typeWiring -> typeWiring.typeName("Droid")
613+
.dataFetcher("friends", StarWarsData.getFriendsDataFetcher())
614+
)
615+
// or full builder syntax if that takes your fancy
616+
.type(
617+
newTypeWiring("Character")
618+
.typeResolver(StarWarsData.getCharacterTypeResolver())
619+
.build()
620+
)
621+
.build();
622+
}
623+
624+
625+
```
626+
627+
NOTE: IDL is not currently part of the [formal graphql spec](https://facebook.github.io/graphql/#sec-Appendix-Grammar-Summary.Query-Document).
628+
The implementation in this library is based off the [reference implementation](https://github.com/graphql/graphql-js). However plenty of
629+
code out there is based on this IDL syntax and hence you can be fairly confident that you are building on solid technology ground.
630+
631+
521632
#### Contributions
522633

523634
Every contribution to make this project better is welcome: Thank you!

‎src/main/java/graphql/execution/ExecutionStrategy.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ public abstract class ExecutionStrategy {
4747
* Handle exceptions which occur during data fetching. By default, add all exceptions to the execution context's
4848
* error's. Subclasses may specify custom handling, e.g. of different behavior with different exception types (e.g.
4949
* re-throwing certain exceptions).
50-
* @param executionContext
51-
* @param fieldDef
52-
* @param argumentValues
53-
* @param e
50+
*
51+
* @param executionContext the execution context in play
52+
* @param fieldDef the field definition
53+
* @param argumentValues the map of arguments
54+
* @param e the exception that occurred
5455
*/
5556
protected void handleDataFetchingException(
5657
ExecutionContext executionContext,
@@ -69,11 +70,10 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, Execut
6970
DataFetchingEnvironment environment = new DataFetchingEnvironmentImpl(
7071
parameters.source(),
7172
argumentValues,
72-
executionContext.getRoot(),
7373
fields,
7474
fieldDef.getType(),
7575
type,
76-
executionContext.getGraphQLSchema()
76+
executionContext
7777
);
7878

7979
Instrumentation instrumentation = executionContext.getInstrumentation();

‎src/main/java/graphql/execution/batched/BatchedExecutionStrategy.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,10 @@ private List<GraphQLExecutionNodeValue> fetchData(ExecutionContext executionCont
255255
DataFetchingEnvironment environment = new DataFetchingEnvironmentImpl(
256256
sources,
257257
argumentValues,
258-
executionContext.getRoot(),
259258
fields,
260259
fieldDef.getType(),
261260
parentType,
262-
executionContext.getGraphQLSchema()
261+
executionContext
263262
);
264263

265264
List<Object> values;

‎src/main/java/graphql/execution/batched/UnbatchedDataFetcher.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public Object get(DataFetchingEnvironment environment) {
3535
environment.getFields(),
3636
environment.getFieldType(),
3737
environment.getParentType(),
38-
environment.getGraphQLSchema());
38+
environment.getGraphQLSchema(),
39+
environment.getFragmentsByName(),
40+
environment.getExecutionId());
3941
results.add(delegate.get(singleEnv));
4042
}
4143
return results;

‎src/main/java/graphql/introspection/Introspection.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package graphql.introspection;
22

33

4+
import graphql.language.AstValueHelper;
5+
import graphql.language.AstPrinter;
46
import graphql.schema.DataFetcher;
57
import graphql.schema.DataFetchingEnvironment;
68
import graphql.schema.GraphQLArgument;
@@ -11,6 +13,7 @@
1113
import graphql.schema.GraphQLFieldsContainer;
1214
import graphql.schema.GraphQLInputObjectField;
1315
import graphql.schema.GraphQLInputObjectType;
16+
import graphql.schema.GraphQLInputType;
1417
import graphql.schema.GraphQLInterfaceType;
1518
import graphql.schema.GraphQLList;
1619
import graphql.schema.GraphQLNonNull;
@@ -98,15 +101,19 @@ public enum TypeKind {
98101
.dataFetcher(environment -> {
99102
if (environment.getSource() instanceof GraphQLArgument) {
100103
GraphQLArgument inputField = environment.getSource();
101-
return inputField.getDefaultValue() != null ? inputField.getDefaultValue().toString() : null;
104+
return inputField.getDefaultValue() != null ? print(inputField.getDefaultValue(), inputField.getType()) : null;
102105
} else if (environment.getSource() instanceof GraphQLInputObjectField) {
103106
GraphQLInputObjectField inputField = environment.getSource();
104-
return inputField.getDefaultValue() != null ? inputField.getDefaultValue().toString() : null;
107+
return inputField.getDefaultValue() != null ? print(inputField.getDefaultValue(), inputField.getType()) : null;
105108
}
106109
return null;
107110
}))
108111
.build();
109112

113+
private static String print(Object value, GraphQLInputType type) {
114+
return AstPrinter.printAst(AstValueHelper.astFromValue(value, type));
115+
}
116+
110117

111118
public static GraphQLObjectType __Field = newObject()
112119
.name("__Field")

0 commit comments

Comments
 (0)