Skip to content

Commit 5add9bf

Browse files
committed
add code examples
1 parent d6e0395 commit 5add9bf

File tree

1 file changed

+201
-7
lines changed

1 file changed

+201
-7
lines changed

site/code/index.html.js

Lines changed: 201 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,220 @@ Many different programming languages support GraphQL. This list contains some of
2929
3030
### JavaScript
3131
32-
- [GraphQL.js](/graphql-js/) ([github](https://github.com/graphql/graphql-js/)) ([npm](https://www.npmjs.com/package/graphql)): The reference implementation of the GraphQL specification, designed for running a GraphQL server in a Node.js environment.
33-
- [express-graphql](/graphql-js/running-an-express-graphql-server/) ([github](https://github.com/graphql/express-graphql)) ([npm](https://www.npmjs.com/package/express-graphql)): The reference implementation of a GraphQL API server over an Express webserver. You can use this to run GraphQL in conjunction with a regular Express webserver, or as a standalone GraphQL server.
34-
- [graphql-relay](https://github.com/graphql/graphql-relay-js) ([npm](https://www.npmjs.com/package/graphql-relay)): A server library to help construct a Node.js GraphQL API that supports [Relay](https://facebook.github.io/relay/).
35-
- [Apollo Server](http://dev.apollodata.com/tools/apollo-server/index.html) ([github](https://github.com/apollostack/apollo-server)) ([npm](https://www.npmjs.com/package/apollo-server)): A GraphQL server that works with all Node.js HTTP server frameworks: Express, Connect, HAPI and Koa.
32+
#### [GraphQL.js](/graphql-js/) ([github](https://github.com/graphql/graphql-js/)) ([npm](https://www.npmjs.com/package/graphql))
33+
34+
The reference implementation of the GraphQL specification, designed for running GraphQL in a Node.js environment.
35+
36+
To run a \`GraphQL.js\` hello world script from the command line:
37+
38+
\`\`\`bash
39+
npm install graphql
40+
\`\`\`
41+
42+
Then run \`node hello.js\` with this code in \`hello.js\`:
43+
44+
\`\`\`js
45+
var { graphql, buildSchema } = require('graphql');
46+
47+
var schema = buildSchema(\`
48+
type Query {
49+
hello: String
50+
}
51+
\`);
52+
53+
var root = { hello: () => 'Hello world!' };
54+
55+
graphql(schema, '{ hello }', root).then((response) => {
56+
console.log(response);
57+
});
58+
\`\`\`
59+
60+
#### [express-graphql](/graphql-js/running-an-express-graphql-server/) ([github](https://github.com/graphql/express-graphql)) ([npm](https://www.npmjs.com/package/express-graphql))
61+
62+
The reference implementation of a GraphQL API server over an Express webserver. You can use this to run GraphQL in conjunction with a regular Express webserver, or as a standalone GraphQL server.
63+
64+
To run an \`express-graphql\` hello world server:
65+
66+
\`\`\`bash
67+
npm install express express-graphql graphql
68+
\`\`\`
69+
70+
Then run \`node server.js\` with this code in \`server.js\`:
71+
72+
\`\`\`js
73+
var express = require('express');
74+
var graphqlHTTP = require('express-graphql');
75+
var { buildSchema } = require('graphql');
76+
77+
var schema = buildSchema(\`
78+
type Query {
79+
hello: String
80+
}
81+
\`);
82+
83+
var root = { hello: () => 'Hello world!' };
84+
85+
var app = express();
86+
app.use('/graphql', graphqlHTTP({
87+
schema: schema,
88+
rootValue: root,
89+
graphiql: true,
90+
}));
91+
app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));
92+
\`\`\`
93+
94+
#### [Apollo Server](http://dev.apollodata.com/tools/apollo-server/index.html) ([github](https://github.com/apollostack/apollo-server)) ([npm](https://www.npmjs.com/package/apollo-server))
95+
96+
A GraphQL server that works with Node.js.
97+
98+
To run a hello world server with Apollo Server:
99+
100+
\`\`\`bash
101+
npm install apollo-server body-parser express graphql graphql-tools
102+
\`\`\`
103+
104+
Then run \`node server.js\` with this code in \`server.js\`:
105+
106+
\`\`\`js
107+
var express = require('express');
108+
var bodyParser = require('body-parser');
109+
var { apolloExpress, graphiqlExpress } = require('apollo-server');
110+
var { makeExecutableSchema } = require('graphql-tools');
111+
112+
var typeDefs = [\`
113+
type Query {
114+
hello: String
115+
}
116+
117+
schema {
118+
query: Query
119+
}\`];
120+
121+
var resolvers = {
122+
Query: {
123+
hello(root) {
124+
return 'world';
125+
}
126+
}
127+
};
128+
129+
var schema = makeExecutableSchema({typeDefs, resolvers});
130+
var app = express();
131+
app.use('/graphql', bodyParser.json(), apolloExpress({schema}));
132+
app.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}));
133+
app.listen(4000, () => console.log('Now browse to localhost:4000/graphiql'));
134+
\`\`\`
135+
136+
Apollo Server also supports all Node.js HTTP server frameworks: Express, Connect, HAPI and Koa.
36137
37138
### Ruby
38139
39-
- [graphql-ruby](https://github.com/rmosolgo/graphql-ruby): A Ruby library for building GraphQL APIs. Built-in support for Relay and Rails.
140+
#### [graphql-ruby](https://github.com/rmosolgo/graphql-ruby)
141+
142+
A Ruby library for building GraphQL APIs.
143+
144+
To run a hello world script with \`graphql-ruby\`:
145+
146+
\`\`\`bash
147+
gem install graphql
148+
\`\`\`
149+
150+
Then run \`ruby hello.rb\` with this code in \`hello.rb\`:
151+
152+
\`\`\`ruby
153+
require 'graphql'
154+
155+
QueryType = GraphQL::ObjectType.define do
156+
name 'Query'
157+
field :hello do
158+
type types.String
159+
resolve -> (obj, args, ctx) { 'Hello world!' }
160+
end
161+
end
162+
163+
Schema = GraphQL::Schema.define do
164+
query QueryType
165+
end
166+
167+
puts Schema.execute('{ hello }')
168+
\`\`\`
169+
170+
There are also nice bindings for Relay and Rails.
40171
41172
### Python
42173
43-
- [Graphene](http://graphene-python.org/) ([github](https://github.com/graphql-python/graphene)): A Python library for building GraphQL APIs. Built-in support for [Relay](https://facebook.github.io/relay/), Django, SQLAlchemy, and Google App Engine.
174+
#### [Graphene](http://graphene-python.org/) ([github](https://github.com/graphql-python/graphene))
175+
176+
A Python library for building GraphQL APIs.
177+
178+
To run a Graphene hello world script:
179+
180+
\`\`\`bash
181+
pip install graphene
182+
\`\`\`
183+
184+
Then run \`python hello.py\` with this code in \`hello.py\`:
185+
186+
\`\`\`python
187+
import graphene
188+
189+
class Query(graphene.ObjectType):
190+
hello = graphene.String()
191+
192+
def resolve_hello(self, args, info):
193+
return 'Hello world!'
194+
195+
schema = graphene.Schema(query=Query)
196+
result = schema.execute('{ hello }')
197+
print(result.data['hello'])
198+
\`\`\`
199+
200+
There are also nice bindings for [Relay](https://facebook.github.io/relay/), Django, SQLAlchemy, and Google App Engine.
44201
45202
### Scala
46203
47204
- [Sangria](http://sangria-graphql.org/) ([github](https://github.com/sangria-graphql/sangria)): A Scala GraphQL library that supports [Relay](https://facebook.github.io/relay/).
48205
49206
### Java
50207
51-
- [graphql-java](https://github.com/graphql-java/graphql-java): A Java library for building GraphQL APIs.
208+
#### [graphql-java](https://github.com/graphql-java/graphql-java)
209+
210+
A Java library for building GraphQL APIs.
211+
212+
Code that executes a hello world GraphQL query with \`graphql-java\`:
213+
214+
\`\`\`java
215+
import graphql.schema.GraphQLObjectType;
216+
import graphql.schema.GraphQLSchema;
217+
218+
import static graphql.Scalars.GraphQLString;
219+
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
220+
import static graphql.schema.GraphQLObjectType.newObject;
221+
222+
public class HelloWorld {
223+
224+
public static void main(String[] args) {
225+
226+
GraphQLObjectType queryType = newObject()
227+
.name("helloWorldQuery")
228+
.field(newFieldDefinition()
229+
.type(GraphQLString)
230+
.name("hello")
231+
.staticValue("Hello world!"))
232+
.build();
233+
234+
GraphQLSchema schema = GraphQLSchema.newSchema()
235+
.query(queryType)
236+
.build();
237+
Map<String, Object> result = new GraphQL(schema).execute("{hello}").getData();
238+
239+
System.out.println(result);
240+
// Prints: {hello=world}
241+
}
242+
}
243+
\`\`\`
244+
245+
See [the graphql-java docs](https://github.com/graphql-java/graphql-java) for more information on setup.
52246
53247
### Go
54248

0 commit comments

Comments
 (0)