sirsavary / fastify-graphql Goto Github PK
View Code? Open in Web Editor NEWRun an Apollo Server on Fastify.
Home Page: https://www.npmjs.com/package/fastify-graphql
License: MIT License
Run an Apollo Server on Fastify.
Home Page: https://www.npmjs.com/package/fastify-graphql
License: MIT License
Should be really cool to see apollographql/apollo-server#626 fixed using this repo.
As you already done switch to TypeScript, it seems to be on the road of apollo-server-fastify
;)
Thanks for your works and @coopnd one !
Can you please release a new patch version ?
Thanks !
Ran across the following error when trying out fastify-graphql
, here's the simplest reproduction of it I can make :)
First:
npm i fastify graphql fastify-graphql graphql-tools
Then the small test server:
const {graphqlFastify} = require('fastify-graphql')
const {makeExecutableSchema} = require('graphql-tools')
const fastify = require('fastify')({
logger: true
})
const typeDefs = `
type Test {
id: ID
}
type Query {
test: [Test]
}
schema {
query: Query
}
`
const resolvers = {
Query: {
test: () => [{id: '1234'}]
}
}
const schema = makeExecutableSchema({typeDefs, resolvers})
const port = 3000
fastify.register(graphqlFastify, {
prefix: '/graphql',
graphql: {
schema
}
})
const start = async () => {
try {
await fastify.listen(port)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
Run the above and then try this curl:
curl http://localhost:3000/graphql?query=query%20%7B%0A%20%20test%20%7B%0A%20%20%20%20id%0A%20%20%7D%0A%7D
Fastify logs the following error:
{
"level":50,
"time":1528067153535,
"msg":"Promise may not be fulfilled with 'undefined' when statusCode is not 204",
"pid":27185,
"hostname":"some-host",
"reqId":1,
"type":"Error",
"stack":"Error: Promise may not be fulfilled with 'undefined' when statusCode is not 204\n at /some/path/example/node_modules/fastify/lib/handleRequest.js:102:31\n at process._tickCallback (internal/process/next_tick.js:68:7)",
"v":1
}
Once by runHttpQuery
and again by Fastify's bundled JSON serializer.
This is a result of us setting the content-type
header to application/json
. Serializing twice is inefficient and wasteful and the only obvious fix is a custom implementation of runHttpQuery
that does not serialize the GQL response as there appears to be no easy way to disable Fastify's serializer (see fastify/docs/ContentTypeParser)
For the code in question, see GraphQLPlugin.ts#L19
I am trying follow the official doc to use data source in my resolvers.
However, there's no field name dataSources
exists on context
.
I am wondering if the dataSources
is acceptable when I try to register the plugin?
const resolvers = {
Query: {
product: async (_source: any, args: any, context: any) => {
console.log(args)
console.log(context) // no dataSources in the context
return {
id: 1
}
}
},
};
server.register(graphqlFastify, {
prefix: '/graphql',
graphql: {
schema,
dataSources: () => {
console.log('The log doesn\'t print')
return {
productAPI: new ProductAPI(),
};
},
context: () => {
return {
token: 'foo',
};
},
tracing: true,
cacheControl: {
defaultMaxAge: 500,
stripFormattedExtensions: false,
calculateCacheControlHeaders: false,
},
cache: new RedisCache({
host: '127.0.0.1'
})
},
});
https://www.apollographql.com/docs/apollo-server/features/data-sources.html
Hello,
I just noticed you require graphql on your dependencies. Is this really necessary? I don't see where it's used in the project. I just installed the 0.13 version of graphql in my project and I had to downgrade the version in order to use your plugin.
Thanks!
Failed to send response header(s)
/**
* @public
* @async
* (Query) Get current user information
* @returns {Promise<Object>}
*/
Query.test = async ( root, args, ctx, info ) => {
/** @type {(fastify#FastifyReply|FastifyResponse)} */
const res = ctx.res;
res.header('X-test', 'changed');
return true;
};
Response header:
< HTTP/1.1 200 OK
< X-XSS-Protection: 1; mode=block
< Vary: Origin
< Access-Control-Allow-Credentials: true
< x-test: changed
< content-type: application/json; charset=utf-8
< content-length: 2
< Date: Thu, 14 Feb 2019 17:18:54 GMT
< Connection: keep-alive
Header X-test
wasn't sent to response, same issues with cookies.
After registering fastify-cors and graphqlfastify as plugins:
fastify.register(require('fastify-cors'))
fastify.register(graphqlFastify, { prefix: '/graphql', graphql: { schema: require('./schema'), rootValue:
require('./resolvers'), context: {} }})
requests to /graphql does not contain response header for Allow-Acces-Control-Header: *
and the browser block access to the fetch:
Access to fetch at 'http://localhost:3003/graphql' from origin 'http://localhost:3001' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
ApolloError.js:58 Uncaught (in promise) Error: Network error: Failed to fetch
at new ApolloError (ApolloError.js:58)
at QueryManager.js:526
at QueryManager.js:986
at Array.forEach ()
at QueryManager.js:985
at Map.forEach ()
at QueryManager.broadcastQueries (QueryManager.js:981)
at QueryManager.js:481
cannot find any related documentation or Issue....
help ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.