apollo-server

Apollo / GraphQL Same Query with Different Parameters

两盒软妹~` 提交于 2019-12-11 03:36:08
问题 How would I go about having an Apollo query with different parameters. Let's say my app has the concept of users and I want my API to be able to find a user either by ID or username. It doesn't seem I can do this type Query { user(id: ID!): User user(username: String!): User } do I have to resort to something like this type Query { userById(id: ID!): User userByUsername(username: String!): User } 回答1: Create a new input type with ID and username as fields and specify it as parameter. type

Unable to use Fragments on GraphQL-yoga with Primsa

最后都变了- 提交于 2019-12-10 23:39:48
问题 I am using graphql-yoga with Prisma and Prisma-Bindings I'm trying to add a fragment to my resolver so that a specific field (id in this situation) is always fetched when the user asks for a custom field, costsToDate. This is so i can make some additional queries needed to build the result for that field, and i need the ID of the object for that. Unfortunatley i can't seem to get it to work, and the documentations seems a little lacking on the specifics with graphql-yoga and Prisma. Here is

How to use or resolve enum types with graphql-tools?

泄露秘密 提交于 2019-12-07 15:22:53
问题 I cannot find anywhere in the graphql-tools documentation how one should go about utilizing enum types in schemas that are fed to makeExecutableSchema . Anyone have a clue how this done? Example code: enum Color { RED GREEN BLUE } type Car { color: Color! } What would the resolver for Color look like? 回答1: You wouldn't write a resolver for Color . Here's a simple, runnable example: const bodyParser = require('body-parser'); const { graphqlExpress, graphiqlExpress } = require('graphql-server

How to parse GraphQL request string into an object

痴心易碎 提交于 2019-12-05 07:25:24
I am running Apollo lambda server for GraphQL. I want to intercept the GraphQL query/mutation from the POST request body and parse it so I can find out which query/mutation the request is asking for. The environment is Node.js. The request isn't JSON, it's GraphQL query language. I've looked around to try and find a way to parse this into an object that I can navigate but I'm drawing a blank. The Apollo server must be parsing it somehow to direct the request. Does anyone know a library that will do this or pointers on how I can parse the request? Examples of request bodies and what I want to

Apollo Server - Confusion about cache/datasource options

房东的猫 提交于 2019-12-03 16:43:33
The docs ( https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Using-Memcached-Redis-as-a-cache-storage-backend ) show code like this: const { RedisCache } = require('apollo-server-cache-redis'); const server = new ApolloServer({ typeDefs, resolvers, cache: new RedisCache({ host: 'redis-server', // Options are passed through to the Redis client }), dataSources: () => ({ moviesAPI: new MoviesAPI(), }), }); I was wondering how that cache key is used, considering it seems like the caching is actually custom implemented in something like MoviesAPI() and then used via

How do I handle deletes in react-apollo

十年热恋 提交于 2019-12-03 08:31:57
问题 I have a mutation like mutation deleteRecord($id: ID) { deleteRecord(id: $id) { id } } and in another location I have a list of elements. Is there something better I could return from the server, and how should I update the list? More generally, what is best practice for handling deletes in apollo/graphql? 回答1: I am not sure it is good practise style but here is how I handle the deletion of an item in react-apollo with updateQueries: import { graphql, compose } from 'react-apollo'; import gql

How to handle async errors correctly?

非 Y 不嫁゛ 提交于 2019-12-02 13:51:42
问题 When making a GraphQL query, and the query fails, Apollo solves this by having a data-object and an error-object. When an async error is happening, we get the same functionality with one data-object and one error-object. But, this time we get an UnhandledPromiseRejectionWarning too, with information about: DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. . So,

Accessing Request object in Apollo Server with ExpressJS

蓝咒 提交于 2019-12-02 09:37:26
Is there any way of accessing the request object from the underlying express app in Apollo Server The context configuration parameter can be either an object, a function that returns the object, or a function that returns a promise to return the object. This function would get the HTTP request as a parameter, and could be defined like so: const apolloServer = new ApolloServer({ schema, context: async ({ req }) => { const something = getSomething(req) return { something } }, }) apolloServer.applyMiddleware({ app, path: '/graphql' }) const { appPort } = serviceFunc.getAccessData() app.listen({

apollo-server-express CORS issue

我们两清 提交于 2019-12-01 19:07:50
So I am migrating to apollo-server-express 2.3.3 ( I was using 1.3.6 ) I've followed several guides, making the necessary tweaks but am stuck in a CORS issue. According to the docs you have to use the applyMiddleware function to wire up the apollo server with express. I am currently doing the following: const app = express(); // CORS configuration const corsOptions = { origin: 'http://localhost:3000', credentials: true } app.use(cors(corsOptions)) // Setup JWT authentication middleware app.use(async (req, res, next) => { const token = req.headers['authorization']; if(token !== "null"){ try {

Log apollo-server GraphQL query and variables per request

和自甴很熟 提交于 2019-11-30 15:20:25
When using apollo-server 2.2.1 or later, how can one log, for each request, the query and the variables? This seems like a simple requirement and common use case, but the documentation is very vague , and the query object passed to formatResponse no longer has the queryString and variables properties. Amit's answer works (today), but IMHO it is a bit hacky and it may not work as expected in the future, or it may not work correctly in some scenarios. For instance, the first thing that I thought when I saw it was: "that may not work if the query is invalid", it turns out that today it does work