Skip to content

Commit e591d85

Browse files
author
Kevin Lacker
authored
Merge pull request graphql#66 from lacker/graphqljs
Misc small docs fixes
2 parents 9771b9f + 2c1bb0d commit e591d85

9 files changed

+120
-17
lines changed

site/code/index.html.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Many different programming languages support GraphQL. This list contains some of
3030
## GraphQL Server Libraries
3131
3232
- [GraphQL.js](/graphql-js/getting-started/) ([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/getting-started/) ([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.
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.
3434
- [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.
3535
- [graphql-ruby](https://github.com/rmosolgo/graphql-ruby): A Ruby library for building GraphQL APIs. Built-in support for Relay and Rails.
3636
- [graphql-java](https://github.com/graphql-java/graphql-java): A Java library for building GraphQL APIs.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: express-graphql
3+
layout: ../_core/CodeLayout
4+
category: API Reference
5+
permalink: /docs/api-reference-express-graphql/
6+
sublinks: graphqlHTTP
7+
next: /docs/api-reference-graphql/
8+
---
9+
10+
The `express-graphql` module provides a simple way to create an [Express](https://expressjs.com/) server that runs a GraphQL API.
11+
12+
```js
13+
import graphqlHTTP from 'express-graphql'; // ES6
14+
var graphqlHTTP = require('graphql'); // CommonJS
15+
```
16+
17+
### graphqlHTTP
18+
19+
```js
20+
graphqlHTTP({
21+
schema: GraphQLSchema,
22+
graphiql?: ?boolean,
23+
rootValue?: ?any,
24+
context?: ?any,
25+
pretty?: ?boolean,
26+
formatError?: ?Function,
27+
validationRules?: ?Array<any>,
28+
}): Middleware
29+
```
30+
31+
Constructs an Express application based on a GraphQL schema.
32+
33+
See the [express-graphql tutorial](/graphql-js/running-an-express-graphql-server/) for sample usage.
34+
35+
See the [GitHub README](https://github.com/graphql/express-graphql) for more extensive documentation of the details of this method.

site/docs/APIReference-Utilities.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: graphql/utilities
33
layout: ../_core/CodeLayout
44
category: API Reference
55
permalink: /docs/api-reference-utilities/
6-
sublinks: introspectionQuery,buildClientSchema,printSchema,printIntrospectionSchema,buildASTSchema,typeFromAST,astFromValue,TypeInfo,isValidJSValue,isValidLiteralValue
6+
sublinks: introspectionQuery,buildClientSchema,printSchema,printIntrospectionSchema,buildASTSchema,typeFromAST,astFromValue,TypeInfo,isValidJSValue,isValidLiteralValue,buildSchema
77
next: /docs/api-reference-validation/
88
---
99

@@ -37,6 +37,12 @@ var GraphQLUtilities = require('graphql/utilities'); // CommonJS
3737
*Schema Language*
3838

3939
<ul class="apiIndex">
40+
<li>
41+
<a href="#buildschema">
42+
<pre>function buildSchema</pre>
43+
Builds a Schema object from GraphQL schema language.
44+
</a>
45+
</li>
4046
<li>
4147
<a href="#printschema">
4248
<pre>function printSchema</pre>
@@ -126,6 +132,14 @@ server-internal mechanisms.
126132

127133
## Schema Representation
128134

135+
### buildSchema
136+
137+
```js
138+
function buildSchema(source: string | Source): GraphQLSchema {
139+
```
140+
141+
Creates a GraphQLSchema object from GraphQL schema language. The schema will use default resolvers. For more detail on the GraphQL schema language, see the [schema language docs](/learn/schema/) or this [schema language cheat sheet](https://wehavefaces.net/graphql-shorthand-notation-cheatsheet-17cd715861b6#.9oztv0a7n).
142+
129143
### printSchema
130144
131145
```js

site/docs/Guides-ConstructingTypes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Constructing Types
33
layout: ../_core/CodeLayout
44
category: Advanced Guides
55
permalink: /graphql-js/constructing-types/
6-
next: /docs/api-reference-graphql/
6+
next: /docs/api-reference-express-graphql/
77
---
88

99
For many apps, you can define a fixed schema when the application starts, and define it using GraphQL schema language. In some cases, it's useful to construct a schema programmatically. You can do this using the `GraphQLSchema` constructor.
@@ -41,7 +41,7 @@ var fakeDatabase = {
4141
};
4242

4343
var root = {
44-
user: function({id}) {
44+
user: function ({id}) {
4545
return fakeDatabase[id];
4646
}
4747
};

site/docs/Tutorial-Authentication.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function loggingMiddleware(req, res, next) {
3030
}
3131

3232
var root = {
33-
ip: function(args, request) {
33+
ip: function (args, request) {
3434
return request.ip;
3535
}
3636
};

site/docs/Tutorial-GraphQLClients.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ xhr.responseType = 'json';
3131
xhr.open("POST", "/graphql");
3232
xhr.setRequestHeader("Content-Type", "application/json");
3333
xhr.setRequestHeader("Accept", "application/json");
34-
xhr.onload = function() {
34+
xhr.onload = function () {
3535
console.log('data returned:', xhr.response);
3636
}
37-
xhr.send(JSON.stringify({query:"{ hello }"}));
37+
xhr.send(JSON.stringify({query: "{ hello }"}));
3838
```
3939

4040
You should see the data returned, logged in the console:
@@ -43,6 +43,37 @@ You should see the data returned, logged in the console:
4343
data returned: Object { hello: "Hello world!" }
4444
```
4545

46+
In this example, the query was just a hardcoded string. As your application becomes more complex, and you add GraphQL endpoints that take arguments as described in [Passing Arguments](/graphql-js/passing-arguments/), you will want to construct GraphQL queries using variables in client code. You can do this by including a keyword prefixed with a dollar sign in the query, and passing an extra `variables` field on the payload.
47+
48+
For example, let's say you're running the example server from [Passing Arguments](/graphql-js/passing-arguments/) that has a schema of
49+
50+
```javascript
51+
type Query {
52+
rollDice(numDice: Int!, numSides: Int): [Int]
53+
}
54+
```
55+
56+
You could access this from JavaScript with the code:
57+
58+
```javascript
59+
var dice = 3;
60+
var sides = 6;
61+
var xhr = new XMLHttpRequest();
62+
xhr.responseType = 'json';
63+
xhr.open("POST", "/graphql");
64+
xhr.setRequestHeader("Content-Type", "application/json");
65+
xhr.setRequestHeader("Accept", "application/json");
66+
xhr.onload = function () {
67+
console.log('data returned:', xhr.response);
68+
}
69+
xhr.send(JSON.stringify({
70+
query: "{ rollDice(numDice: $dice, numSides: $sides) }",
71+
variables: { dice: dice, sides: sides },
72+
}));
73+
```
74+
75+
Using this syntax for variables is a good idea because it automatically prevents bugs due to escaping, and it makes it easier to monitor your server.
76+
4677
In general, it will take a bit more time to set up a GraphQL client like Relay, but it's worth it to get more features as your application grows. You might want to start out just using HTTP requests as the underlying transport layer, and switching to a more complex client as your application gets more complex.
4778

4879
At this point you can write a client and server in GraphQL for an API that receives a single string. To do more, you will want to [learn how to use the other basic data types](/graphql-js/basic-types/).

site/docs/Tutorial-Mutations.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ Both mutations and queries can be handled by root resolvers, so the root that im
2525
```javascript
2626
var fakeDatabase = {};
2727
var root = {
28-
setMessage: function({message}) {
28+
setMessage: function ({message}) {
2929
fakeDatabase.message = message;
3030
}
31-
getMessage: function() {
31+
getMessage: function () {
3232
return fakeDatabase.message;
3333
}
3434
};
@@ -87,16 +87,16 @@ var fakeDatabase = {};
8787

8888
// The root provides the top-level API endpoints
8989
var root = {
90-
getMessage: function({author}) {
90+
getMessage: function ({author}) {
9191
return fakeDatabase[author];
9292
},
93-
createMessage: function({message}) {
93+
createMessage: function ({message}) {
9494
if (fakeDatabase[message.author]) {
9595
throw new Error('a message already exists with this author');
9696
}
9797
fakeDatabase[message.author] = message.content;
9898
},
99-
updateMessage: function({message}) {
99+
updateMessage: function ({message}) {
100100
if (!fakeDatabase[message.author]) {
101101
throw new Error('no message exists with this author');
102102
}

site/docs/Tutorial-ObjectTypes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RandomDie {
5050
}
5151

5252
var root = {
53-
getDie: function({numSides}) {
53+
getDie: function ({numSides}) {
5454
return new RandomDie(numSides || 6);
5555
}
5656
}
@@ -111,7 +111,7 @@ class RandomDie {
111111

112112
// The root provides the top-level API endpoints
113113
var root = {
114-
getDie: function({numSides}) {
114+
getDie: function ({numSides}) {
115115
return new RandomDie(numSides || 6);
116116
}
117117
}

site/docs/Tutorial-PassingArguments.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ So far, our resolver functions took no arguments. When a resolver takes argument
2828

2929
```javascript
3030
var root = {
31-
rollDice: function(args) {
31+
rollDice: function (args) {
3232
var output = [];
3333
for (var i = 0; i < args.numDice; i++) {
3434
output.push(1 + Math.floor(Math.random() * (args.numSides || 6)));
@@ -42,7 +42,7 @@ It's convenient to use [ES6 destructuring assignment](https://developer.mozilla.
4242

4343
```javascript
4444
var root = {
45-
rollDice: function({numDice, numSides}) {
45+
rollDice: function ({numDice, numSides}) {
4646
var output = [];
4747
for (var i = 0; i < numDice; i++) {
4848
output.push(1 + Math.floor(Math.random() * (numSides || 6)));
@@ -70,7 +70,7 @@ var schema = buildSchema(`
7070

7171
// The root provides a resolver function for each API endpoint
7272
var root = {
73-
rollDice: function({numDice, numSides}) {
73+
rollDice: function ({numDice, numSides}) {
7474
var output = [];
7575
for (var i = 0; i < numDice; i++) {
7676
output.push(1 + Math.floor(Math.random() * (numSides || 6)));
@@ -99,4 +99,27 @@ When you call this API, you have to pass each argument by name. So for the serve
9999

100100
If you run this code with `node server.js` and browse to [http://localhost:4000/graphql](http://localhost:4000/graphql) you can try out this API.
101101

102+
When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use `$` syntax to define variables in your query, and pass the variables as a separate map.
103+
104+
For example, some JavaScript code that calls our server above is:
105+
106+
```javascript
107+
var dice = 3;
108+
var sides = 6;
109+
var xhr = new XMLHttpRequest();
110+
xhr.responseType = 'json';
111+
xhr.open("POST", "/graphql");
112+
xhr.setRequestHeader("Content-Type", "application/json");
113+
xhr.setRequestHeader("Accept", "application/json");
114+
xhr.onload = function () {
115+
console.log('data returned:', xhr.response);
116+
}
117+
xhr.send(JSON.stringify({
118+
query: "{ rollDice(numDice: $dice, numSides: $sides) }",
119+
variables: { dice: dice, sides: sides },
120+
}));
121+
```
122+
123+
Using `$dice` and `$sides` as variables in GraphQL means we don't have to worry about escaping on the client side.
124+
102125
With basic types and argument passing, you can implement anything you can implement in a REST API. But GraphQL supports even more powerful queries. You can replace multiple API calls with a single API call if you learn how to [define your own object types](/graphql-js/object-types/).

0 commit comments

Comments
 (0)