Open xgqfrms-GitHub opened 7 years ago
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.StaticDataFetcher;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;
public class HelloWorld {
public static void main(String[] args) {
String schema = "type Query{hello: String} schema{query: Query}";
SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
RuntimeWiring runtimeWiring = newRuntimeWiring()
.type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
.build();
SchemaGenerator schemaGenerator = new SchemaGenerator();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
ExecutionResult executionResult = build.execute("{hello}");
System.out.println(executionResult.getData().toString());
// Prints: {hello=world}
}
}
https://www.youtube.com/watch?v=9sc8Pyc51uU
https://gist.github.com/adrianodennanni/c2f0375c11353a0a789425e1be7fb9ba
/*
$ npm i -S graphql
*/
import { GraphQLSchema } from 'graphql';
export default new GraphQLSchema({
query: QueryType,
});
import {
GraphQLList,
GraphQLObjectType,
GraphQLString,
} from 'graphql';
const QueryType = new GraphQLObjectType({
name: 'Query',
description: 'The root of all... queries',
fields: () => (
{
allPeople: {
type: new GraphQLList(PersonType),
resolve: root => {
// Fetch the index of people from the REST API
},
},
person: {
type: PersonType,
args: {
id: {
type: GraphQLString
}
},
resolve: (root, args) => {
// Fetch the person with ID `args.id`
},
}
}
)
});
import {
GraphQLList,
GraphQLObjectType,
GraphQLString,
} from 'graphql';
const PersonType = new GraphQLObjectType({
name: 'Person',
description: 'Somebody that you used to know',
fields: () => (
{
firstName: {
type: GraphQLString,
resolve: person => person.first_name
},
lastName: {
type: GraphQLString,
resolve: person => person.last_name
},
email: {
type: GraphQLString
},
id: {
type: GraphQLString
},
username: {
type: GraphQLString
},
friends: {
type: new GraphQLList(PersonType),
resolve: person => {
// Fetch the friends with the URLs `person.friends`
}
}
}
)
});
http://graphql.org/learn/
http://graphql.org/code/#javascript
Then run
node hello.js
with this code inhello.js
:http://graphql.org/code/#express-graphql-graphql-js-running-an-express-graphql-server-github-https-github-com-graphql-express-graphql-npm-https-www-npmjs-com-package-express-graphql
Then run
node server.js
with this code inserver.js
:https://github.com/apollographql/apollo-server
http://dev.apollodata.com/tools/apollo-server/index.html
http://graphql.org/code/#graphql-server-http-dev-apollodata-com-tools-graphql-server-index-html-github-https-github-com-apollostack-graphql-server-npm-https-www-npmjs-com-package-graphql-server-express
Then run
node server.js
with this code inserver.js
:https://www.howtographql.com/