Skip to content

Commit e143708

Browse files
committed
Add support + docs for exporting the GraphQLSchema
1 parent cf90fe9 commit e143708

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,26 @@ app.use('/graphql', jsonGraphqlExpress(data));
529529
app.listen(PORT);
530530
```
531531

532+
## Schema Export
533+
534+
You also use the export `jsonSchemaBuilder` to get your own copy of the GraphQLSchema:
535+
536+
In node:
537+
```js
538+
import {graphql} from 'graphql';
539+
import {jsonSchemaBuilder} from 'json-graphql-server';
540+
541+
const data = { };
542+
const schema = jsonSchemaBuilder(data);
543+
const query = `[...]`
544+
545+
graphql(schema, query).then(result => {
546+
console.log(result);
547+
});
548+
```
549+
550+
Or available in the global scope when running on a client as `jsonSchemaBuilder`.
551+
532552
## Deployment
533553

534554
Deploy with Heroku or Next.js.

src/client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import GraphQLClientServer from './graphQLClientServer';
2+
import schemaBuilder from './schemaBuilder';
23

34
if (typeof window !== 'undefined') {
45
window.JsonGraphqlServer = GraphQLClientServer;
6+
window.jsonSchemaBuilder = schemaBuilder;
57
}
68

79
export default GraphQLClientServer;

src/jsonGraphqlExpress.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import schemaBuilder from './schemaBuilder';
99
*
1010
* @example
1111
* import express from 'express';
12-
* import { jsonGraphqlExpress } from 'json-graphql-server';
12+
* import jsonGraphqlExpress from 'json-graphql-server';
1313
*
1414
* const data = {
1515
* "posts": [

src/node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import jsonGraphqlExpress from './jsonGraphqlExpress';
2+
import schemaBuilder from './schemaBuilder';
23

4+
export const jsonSchemaBuilder = schemaBuilder;
35
export default jsonGraphqlExpress;

src/schemaBuilder.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,50 @@ import { printSchema } from 'graphql';
33
import getSchemaFromData from './introspection/getSchemaFromData';
44
import resolver from './resolver';
55

6+
/**
7+
* Generates a GraphQL Schema object for your data
8+
*
9+
* @param {any} data
10+
* @returns A GraphQL Schema
11+
*
12+
* @example
13+
* import {graphql} from 'graphql';
14+
* import {jsonSchemaBuilder} from 'json-graphql-server';
15+
*
16+
* const data = {
17+
* "posts": [
18+
* {
19+
* "id": 1,
20+
* "title": "Lorem Ipsum",
21+
* "views": 254,
22+
* "user_id": 123,
23+
* },
24+
* {
25+
* "id": 2,
26+
* "title": "Sic Dolor amet",
27+
* "views": 65,
28+
* "user_id": 456,
29+
* },
30+
* ],
31+
* "users": [
32+
* {
33+
* "id": 123,
34+
* "name": "John Doe"
35+
* },
36+
* {
37+
* "id": 456,
38+
* "name": "Jane Doe"
39+
* }
40+
* ],
41+
* };
42+
*
43+
* const schema = jsonSchemaBuilder(data);
44+
* const query = `[...]`
45+
* graphql(schema, query).then(result => {
46+
* console.log(result);
47+
* });
48+
*
49+
*/
650
export default data =>
751
makeExecutableSchema({
852
typeDefs: printSchema(getSchemaFromData(data)),

0 commit comments

Comments
 (0)