Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Export get plain schema, modify readme with example. #117

Merged
merged 2 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,49 @@ graphql(schema, query).then(result => {

Or available in the global scope when running on a client as `jsonSchemaBuilder`.

## Plain Schema

If you want to use another server type instead of the built in graphql express,
like apollo-server or etc, you can expose the plain schema to be built into
an executable schema (there may be version issues otherwise).

This uses the export `getPlainSchema`.

```js
import { ApolloServer } from 'apollo-server';
import { makeExecutableSchema } from '@graphql-tools/schema'; // or graphql-tools
import { applyMiddleware } from 'graphql-middleware';
import { getPlainSchema } from 'json-graphql-server';

const data = { };

// Example middlewares
const logInput = async (resolve, root, args, context, info) => {
console.log(`1. logInput: ${JSON.stringify(args)}`);
const result = await resolve(root, args, context, info);
console.log(`5. logInput`);
return result;
};
const logResult = async (resolve, root, args, context, info) => {
console.log(`2. logResult`);
const result = await resolve(root, args, context, info);
console.log(`4. logResult: ${JSON.stringify(result)}`);
return result;
};

// Leverage getPlainSchema
const schema = applyMiddleware(
makeExecutableSchema(getPlainSchema(data)),
logInput,
logResult
);
const server = new ApolloServer({
schema,
});
server.listen({ port: 3000 });

```

## Deployment

Deploy with Heroku or Next.js.
Expand Down
3 changes: 2 additions & 1 deletion src/node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import jsonGraphqlExpress from './jsonGraphqlExpress';
import schemaBuilder from './schemaBuilder';
import schemaBuilder, { getPlainSchema } from './schemaBuilder';

export const jsonSchemaBuilder = schemaBuilder;
export { getPlainSchema };
export default jsonGraphqlExpress;
7 changes: 7 additions & 0 deletions src/schemaBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ export default (data) =>
resolvers: resolver(data),
logger: { log: (e) => console.log(e) }, // eslint-disable-line no-console
});

// Same as above, simply returning the object before making it executable.
// This lets you use it with a custom apollo server or etc.
export const getPlainSchema = (data) => ({
typeDefs: printSchema(getSchemaFromData(data)),
resolvers: resolver(data),
});