Skip to content

Instantly share code, notes, and snippets.

View nwillc's full-sized avatar

Nwillc nwillc

View GitHub Profile
var categoryGQL = new Graphql("v1/graphql", "{ categories { key name }}");
categoryGQL.execute((response) => loadCategories(response.data.categories));
@GraphQLName("Entity")
public abstract class Entity {
private String key;
@GraphQLField
@GraphQLDescription("The unique identifier")
public String getKey() {
return key;
}
}
@GraphQLName("query)
public final class QuerySchema {
@GraphQLField
public static Entity entity(final DataFetchingEnvironment env, final String key) {
return ((Dao<Entity>)env.getSource()).find(key);
}
}
schema = newSchema()
.query(GraphQLAnnotations.object(QuerySchema.class))
.mutation(GraphQLAnnotations.object(MutationSchema.class))
.build();
@nwillc
nwillc / handler.java
Last active November 20, 2017 04:09
A Ratpack GraphQL query handler
public class GraphQLHandler implements Handler {
// ...
@Override
public void handle(Context context) throws Exception {
context.parse(Map.class).then(payload -> {
Map<String, Object> variables =
(Map<String, Object>) payload.get("variables");
ExecutionInput executionInput =
ExecutionInput.newExecutionInput()
.query(payload.get("query").toString())
@nwillc
nwillc / Company.java
Created November 20, 2017 04:13
A Company
public class Company {
private final String name;
private Integer revenue;
public Company(String name, Integer revenue) {
this.name = name;
this.revenue = revenue;
}
public Integer getRevenue() {
@nwillc
nwillc / schema.graphqls
Created November 20, 2017 04:17
Companies GraphQL IDL schema
schema {
query: QueryType
mutation: MutationType
}
type QueryType {
company( name: ID!) : Company
companies : [ Company ]
}
@nwillc
nwillc / loading.java
Created November 20, 2017 04:22
Loading the Schema IDL
final SchemaParser schemaParser = new SchemaParser();
final TypeDefinitionRegistry registry;
try (final InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("schema.graphqls");
final InputStreamReader streamReader = new InputStreamReader(inputStream)) {
registry = schemaParser.parse(streamReader);
} catch (Exception e) {
throw new IllegalStateException("Could not parse graphql schema", e);
}
@nwillc
nwillc / CompanyQuery.java
Created November 20, 2017 04:28
DataFetcher to get a Company from a Map
public class CompanyQuery implements DataFetcher<Company> {
private final Map<String, Company> companies;
public CompanyFetcher(Map<String, Company> companies) {
this.companies = companies;
}
@Override
public Company get(DataFetchingEnvironment environment) {
return companies.get(environment.getArgument("name"));
@nwillc
nwillc / gist:2a1b3a3cea5e6fdfa5fb68c8f32e9de0
Created November 20, 2017 04:33
Runtime Wiring attaching a fetcher to a query
RuntimeWiring.newRuntimeWiring()
.type("QueryType", wiring -> wiring
.dataFetcher("company", new CompanyQuery(companies))
.dataFetcher("companies", new CompaniesQuery(companies))
)
.type("MutationType", wiring -> wiring
.dataFetcher("company", new CompanyMutation(companies))
.dataFetcher("companyDelete", new CompanyDelete(companies))
)
.build();