Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,117 @@ public Object executeOperation(@RequestBody Map body) {
}
```

### Schema IDL support

This library allows for "schema driven" development of graphql applications.

It allows you to compile a set of schema files into a executable `GraphqlSchema`.


So given a graphql schema input file like :

```graphql

schema {
query: QueryType
}

type QueryType {
hero(episode: Episode): Character
human(id : String) : Human
droid(id: ID!): Droid
}


enum Episode {
NEWHOPE
EMPIRE
JEDI
}

interface Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
}

type Human implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
homePlanet: String
}

type Droid implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
primaryFunction: String
}


```

You could compile and generate an executable schema via

```java
SchemaCompiler schemaCompiler = new SchemaCompiler();
SchemaGenerator schemaGenerator = new SchemaGenerator();

File schemaFile = loadSchema("starWarsSchema.graphqls");

TypeDefinitionRegistry typeRegistry = schemaCompiler.compile(schemaFile);
RuntimeWiring wiring = buildRuntimeWiring();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, wiring);

```

The static schema definition file has the field and type definitions but you need a runtime wiring to make
it a truly executable schema.

The runtime wiring contains `DataFetchers`, `TypeResolvers` and custom `Scalars` that are needed to make a fully
executable schema.

You wire this together using this builder pattern

```java

RuntimeWiring buildRuntimeWiring() {
return RuntimeWiring.newRuntimeWiring()
.scalar(CustomScalar)
// this uses builder function lambda syntax
.type(typeWiring -> typeWiring.typeName("QueryType")
.dataFetcher("hero", new StaticDataFetcher(StarWarsData.getArtoo()))
.dataFetcher("human", StarWarsData.getHumanDataFetcher())
.dataFetcher("droid", StarWarsData.getDroidDataFetcher())
)
.type(typeWiring -> typeWiring.typeName("Human")
.dataFetcher("friends", StarWarsData.getFriendsDataFetcher())
)
// you can use builder syntax if you don't like the lambda syntax
.type(typeWiring -> typeWiring.typeName("Droid")
.dataFetcher("friends", StarWarsData.getFriendsDataFetcher())
)
// or full builder syntax if that takes your fancy
.type(
newTypeWiring("Character")
.typeResolver(StarWarsData.getCharacterTypeResolver())
.build()
)
.build();
}


```

NOTE: IDL is not currently part of the [formal graphql spec](https://facebook.github.io/graphql/#sec-Appendix-Grammar-Summary.Query-Document).
The implementation in this library is based off the [reference implementation](https://github.com/graphql/graphql-js). However plenty of
code out there is based on this IDL syntax and hence you can be fairly confident that you are building on solid technology ground.


#### Contributions

Every contribution to make this project better is welcome: Thank you!
Expand Down
125 changes: 125 additions & 0 deletions src/main/java/graphql/schema/idl/RuntimeWiring.java
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 {

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Member Author

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

Copy link
Copy Markdown
Contributor

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.


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);
}

}


}

70 changes: 70 additions & 0 deletions src/main/java/graphql/schema/idl/ScalarInfo.java
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()));
}

}
Loading