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
var categoryGQL = new Graphql("v1/graphql", "{ categories { key name }}"); | |
categoryGQL.execute((response) => loadCategories(response.data.categories)); |
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
@GraphQLName("Entity") | |
public abstract class Entity { | |
private String key; | |
@GraphQLField | |
@GraphQLDescription("The unique identifier") | |
public String getKey() { | |
return key; | |
} | |
} |
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
@GraphQLName("query) | |
public final class QuerySchema { | |
@GraphQLField | |
public static Entity entity(final DataFetchingEnvironment env, final String key) { | |
return ((Dao<Entity>)env.getSource()).find(key); | |
} | |
} |
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
schema = newSchema() | |
.query(GraphQLAnnotations.object(QuerySchema.class)) | |
.mutation(GraphQLAnnotations.object(MutationSchema.class)) | |
.build(); |
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
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()) |
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
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() { |
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
schema { | |
query: QueryType | |
mutation: MutationType | |
} | |
type QueryType { | |
company( name: ID!) : Company | |
companies : [ Company ] | |
} |
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
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); | |
} |
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
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")); |
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
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(); |