Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 4af2360

Browse files
committed
Added graphql docs, taken from the readme files of the various packages.
1 parent 096a7a4 commit 4af2360

File tree

1 file changed

+255
-3
lines changed

1 file changed

+255
-3
lines changed

src/pages/docs/transport/graphql.md

Lines changed: 255 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,259 @@
11
---
2-
title: "Graphql"
2+
title: "GraphQL"
33
---
44

5-
# Graphql
5+
# GraphQL Api
66

7-
Coming soon ...
7+
_Schema, Resolvers and Utils for GraphQL server with JSAccounts_
8+
9+
[Github](https://github.com/accounts-js/accounts/tree/master/packages/graphql-api) |
10+
[npm](https://www.npmjs.com/package/@accounts/graphql-api)
11+
12+
> The GraphQL Api package does not contain any network interface / rest server (e.g. express or koa). it's just a collection of GraphQL schema, resolvers and utils!
13+
14+
## How to use this package?
15+
16+
This package exports GraphQL schema and GraphQL resolvers, which you can extend with your existing GraphQL schema server.
17+
18+
Start by installing it from NPM / Yarn:
19+
20+
```bash
21+
// Npm
22+
npm install --save @accounts/server @accounts/graphql-api
23+
24+
// Yarn
25+
yarn add @accounts/server @accounts/graphql-api
26+
```
27+
28+
> This package does not create a transport or anything else, only schema and string and resolvers as object.
29+
30+
Start by configuring your `AccountsServer` as you wish. For example, using MongoDB:
31+
32+
```js
33+
import mongoose from 'mongoose'
34+
import AccountsServer from '@accounts/server'
35+
import AccountsPassword from '@accounts/password'
36+
import MongoDBInterface from '@accounts/mongo'
37+
38+
const db = mongoose.connection
39+
40+
const password = new AccountsPassword()
41+
42+
const accountsServer = new AccountsServer({
43+
{
44+
db: new MongoDBInterface(db),
45+
tokenSecret: 'SECRET',
46+
},
47+
{
48+
password,
49+
}
50+
});
51+
```
52+
53+
Next, import `createAccountsGraphQL` method from this package, and run it with your `AccountsServer`:
54+
55+
```js
56+
import { createAccountsGraphQL } from '@accounts/graphql-api';
57+
58+
const accountsGraphQL = createAccountsGraphQL(accountsServer);
59+
```
60+
61+
Now, add `accountsGraphQL.typeDefs` to your schema definition (just before using it with `makeExecutableSchema`), and extend your resolvers object with `accountsGraphQL.resolvers`, for example:
62+
63+
```js
64+
import { makeExecutableSchema } from 'graphql-tools';
65+
import { merge } from 'lodash';
66+
67+
const typeDefs = [
68+
`
69+
type Query {
70+
myQuery: String
71+
}
72+
73+
type Mutation {
74+
myMutation: String
75+
}
76+
77+
schema {
78+
query: Query,
79+
mutation: Mutation
80+
}
81+
`,
82+
accountsGraphQL.typeDefs,
83+
];
84+
85+
let myResolvers = {
86+
Query: {
87+
myQuery: () => 'Hello',
88+
},
89+
Mutation: {
90+
myMutation: () => 'Hello',
91+
},
92+
};
93+
94+
const schema = makeExecutableSchema({
95+
resolvers: merge(accountsGraphQL.resolvers, myResolvers),
96+
typeDefs,
97+
});
98+
```
99+
100+
The last step is to extend your `graphqlExpress` with a context middleware, that extracts the authentication token from the HTTP request, so AccountsServer will automatically validate it:
101+
102+
```js
103+
import { accountsContext } from '@accounts/graphql-api';
104+
105+
app.use(
106+
GRAPHQL_ROUTE,
107+
bodyParser.json(),
108+
graphqlExpress(request => {
109+
return {
110+
context: {
111+
...accountsContext(request),
112+
// your context
113+
},
114+
schema,
115+
};
116+
})
117+
);
118+
```
119+
120+
121+
## Authenticating Resolvers
122+
123+
You can authenticate your own resolvers with `JSAccounts` authentication flow, by using `authenticated` method from this package.
124+
125+
This method composer also extends `context` with the current authenticated user!
126+
127+
This is an example for a protected mutation:
128+
129+
```js
130+
import AccountsServer from '@accounts/server';
131+
import { authenticated } from '@accounts/graphql-api';
132+
133+
export const resolver = {
134+
Mutation: {
135+
updateUserProfile: authenticated(AccountsServer, (rootValue, args, context) => {
136+
// Write your resolver here
137+
// context.user - the current authenticated user!
138+
}),
139+
},
140+
};
141+
```
142+
143+
## Customization
144+
145+
This package allow you to customize the GraphQL schema and it's resolvers.
146+
147+
For example, some application main query called `MyQuery` or `RootQuery` instead of query, so you can customize the name, without modifying you application's schema.
148+
149+
These are the available customizations:
150+
151+
- `rootQueryName` (string) - The name of the root query, default: `Query`.
152+
- `rootMutationName` (string) - The name of the root mutation, default: `Mutation`.
153+
- `extend` (boolean) - whether to add `extend` before the root type declaration, default: `true`.
154+
- `withSchemaDefinition` (boolean): whether to add `schema { ... }` declaration to the generation schema, default: `false`.
155+
156+
Pass a second object to `createAccountsGraphQL`, for example:
157+
158+
```js
159+
const myCustomGraphQLAccounts = createSchemaWithAccounts(accountsServer, {
160+
rootQueryName: 'RootQuery',
161+
rootMutationName: 'RootMutation',
162+
});
163+
```
164+
165+
Another possible customization is to modify the name of the authentication header, use it with `accountsContext` (the default is `Authorization`):
166+
167+
```js
168+
context: accountsContext(request, 'MyCustomHeader');
169+
```
170+
171+
## Extending `User`
172+
173+
To extend `User` object with custom fields and logic, add your own GraphQL type definition of `User` with the prefix of `extend`, and add your fields:
174+
175+
```graphql
176+
extend type User {
177+
firstName: String
178+
lastName: String
179+
}
180+
```
181+
182+
And also implement a regular resolver, for the fields you added:
183+
184+
```js
185+
const UserResolver = {
186+
firstName: () => 'Dotan',
187+
lastName: () => 'Simha',
188+
};
189+
```
190+
191+
## Extending `User` during password creation
192+
193+
To extend the user object during the user creation you need to extend the `CreateUserInput` type and add your fields:
194+
195+
```graphql
196+
extend input CreateUserInput {
197+
profile: CreateUserProfileInput!
198+
}
199+
200+
input CreateUserProfileInput {
201+
firstName: String!
202+
lastName: String!
203+
}
204+
```
205+
206+
The user will be saved in the db with the profile key set.
207+
208+
# GraphQL Client
209+
210+
_Client side graphql transport for accounts suite_
211+
212+
[Github](https://github.com/accounts-js/accounts/tree/master/packages/graphql-client) |
213+
[npm](https://www.npmjs.com/package/@accounts/graphql-client)
214+
215+
## Install
216+
217+
```
218+
yarn add @accounts/graphql-client
219+
```
220+
221+
## Usage
222+
223+
```js
224+
import { ApolloClient } from 'apollo-client';
225+
import { AccountsGraphQLClient } from '@accounts/graphql-client';
226+
227+
const apolloClient = new ApolloClient({
228+
// apollo options
229+
});
230+
231+
const accountsGraphQL = new GraphQLClient({
232+
graphQLClient: apolloClient,
233+
// other options
234+
});
235+
```
236+
237+
## Using with Apollo Link
238+
239+
In order to send the accounts token on every request sent to your GraphQL server, apollo requires you to implment an apollo-link. This link is usually quite generic when using accounts-js so we've implmeneted the apollo-link you need and offer it as a utility package.
240+
241+
### Install @accounts/apollo-link
242+
243+
```
244+
yarn add @accounts/apollo-link
245+
```
246+
247+
### Hook it up to the apollo client
248+
249+
```js
250+
import { accountsLink } from '@accounts/apollo-link';
251+
252+
const accountsClient = new AccountsClient( ... );
253+
const authLink = accountsLink(accountsClient);
254+
255+
export const apolloClient = new ApolloClient({
256+
link: ApolloLink.from([authLink, httpLink]),
257+
cache,
258+
});
259+
```

0 commit comments

Comments
 (0)