adonis-apollo
Apollo GraphQL server for AdonisJS 5.
Maintained by Zakodium
:warning: This module is unstable and in active development. Use at your own risk. |
---|
Installation
npm i adonis-apollo
node ace configure adonis-apollo
Then add the following to the "metaFiles"
array in .adonisrc.json
:
{
"pattern": "app/Schemas/*",
"reloadServer": true
}
Usage
Bind the apollo server to your AdonisJs application.
In start/routes.ts
:
import ApolloServer from '@ioc:Zakodium/Apollo/Server';
ApolloServer.applyMiddleware();
Route.group(() => {
ApolloServer.applyMiddleware();
}).middleware('auth');
Troubleshooting
Error: Query root type must be provided
Apollo requires a query root type to be defined in your schema.
To fix this error, create a file app/Schemas/SomeSchema.graphql
with at least
a Query
type.
For example:
type Query {
hello: String!
}
BadRequestError: This operation has been blocked as a potential Cross-Site Request Forgery (CSRF)
This error may happen if you try to access the GraphQL endpoint from a browser.
Make sure forceContentNegotiationTo
is not unconditionally set to 'application/json'
in config/app.ts
.
You can either disable this option or set it to a function that ignores the GraphQL route.
Configuration
Landing page
To configure the default landing page, you can pass apolloProductionLandingPageOptions
or apolloLocalLandingPageOptions
to the config. Another possibility is to
override the plugins
config in config/apollo.ts
.
The default configuration is:
import {
ApolloServerPluginLandingPageLocalDefault,
ApolloServerPluginLandingPageProductionDefault,
} from '@apollo/server/plugin/landingPage/default';
const plugins = [
Env.get('NODE_ENV') === 'production'
? ApolloServerPluginLandingPageProductionDefault({
footer: false,
...apolloProductionLandingPageOptions,
})
: ApolloServerPluginLandingPageLocalDefault({
footer: false,
...apolloLocalLandingPageOptions,
}),
];
See the Apollo Graphql documentation to
learn how to customize or disable the landing page.
Scalars
All the resolvers from graphql-scalars
are installed automatically.
To enable any of the scalar types documented in graphql-scalars
,
for example DateTime
, just add a scalar line to your schema:
scalar DateTime
Uploads
To enable support for inline multipart/form-data uploads using graphql-upload:
- Set
enableUploads: true
in config/apollo.ts
. - Update the config of the body parser in
config/bodyparser.ts
by adding your GraphQL route (by default: /graphql
) to the multipart.processManually
array. - Add the Upload scalar to your schema:
scalar Upload
. - Make sure your GraphQL upload client sends the
'Apollo-Require-Preflight'
header, otherwise Apollo will reject multipart requests
to prevent CSRF attacks.
License
MIT