graphql-js

GraphQL query and check the returned data

回眸只為那壹抹淺笑 提交于 2019-12-24 07:01:51
问题 I would like to have a query containing a check, for example when querying users, only users that have a non-null email are wanted. How could I add such check? Thanks! users { id, username, email } 回答1: GraphQL fields can have arguments. So you could pass an argument to your users field, named onlyNonNullEmail: boolean : type Query { users(onlyNonNullEmail: Boolean) { ... } } Then, inside your resolve function: users: (_, args, context, ast) => { const { onlyNonNullEmail } = args; // If

How to handle hyphens in GraphQL Schema definitions

久未见 提交于 2019-12-24 01:17:06
问题 My mongoose schema is as follows var ImageFormats = new Schema({ svg : String, png-xlarge : String, png-small : String }); When I translate this into a GraphQL Schema, this is what I try export var GQImageFormatsType: ObjectType = new ObjectType({ name: 'ImageFormats', fields: { svg : { type: GraphQLString }, 'png-xlarge': { type: GraphQLString }, 'png-small' : { type: GraphQLString } } }); GraphQL Returns the following error: Error: Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "png-xlarge

Dynamic field in graphql object type

喜夏-厌秋 提交于 2019-12-24 00:58:08
问题 I have some GraphQL types: const Atype = new GraphQLObjectType({ name: 'Atype', fields: { data: { type: ADataType }, error: { type: GraphQLString }, } }) and const Btype = new GraphQLObjectType({ name: 'Btype', fields: { data: { type: BDataType }, error: { type: GraphQLString }, } }) It looks redundant because only data fields are different... How can I solve it in more elegant way in GraphQL ? 回答1: I created a new Type named Mixed just to solve similar issue., Mixed works as mongoose Mixed

Edge.node field type must be Output Type but got: undefined

我只是一个虾纸丫 提交于 2019-12-23 10:58:27
问题 I'm trying to set up a node.js server with a Relay-compatible GraphQL schema. When trying to validate or load the schema I get the following error: EventEdge.node field type must be Output Type but got: undefined. This is caused by having a connection type for for the Event type in one of the other type definitions. I won't post the whole schema because it's quite verbose but when the connection field is commented out the schema is loaded correctly and no errors are thrown. I've included an

Data null after saving entity with Moongose and GraphQL

时光怂恿深爱的人放手 提交于 2019-12-23 02:32:33
问题 When saving an entity with mongoose and graphql the following happens: First method to save: create(budgetProps){ const budget = new Budget(budgetProps); return budget.save(); } The result is as follows: { "data": { "addBudget": { "_id": "59fbdefaa7b0a81180dd2c9c", "tiempoAproximado": 2245.5, "User": { "name": null, "organization": null }, "Vehicle": { "name": null, "type": null } } } } Using this method: create(budgetProps){ const budget = new Budget(budgetProps); return budget.save().then(

Authorization in GraphQL servers

情到浓时终转凉″ 提交于 2019-12-21 04:32:08
问题 How to handle Authorization in GraphQL servers? Shall I pass the JWT token in the Authentication header of every requests and check for the authorized user after resolve() and check for the role of user on every query and mutation 回答1: Introduction First of all, a common approach for authentication as you state is using a signed JWT that contains the id of the user making the request. Now let's have a look at the different parameters we can use when considering the authorization of a given

Dynamic (Unique) Objects in GraphQl

梦想的初衷 提交于 2019-12-20 12:04:10
问题 I'm looking at graphql. Is it possible to define an object with arbitrary attributes? Let's say I have some data like: editOptions : { boxes : 3 , size : { width: 23,height:32} , color: #434343 }, etc...} and this is in: { ... , box : { editOptions : {...} }, ... } Let's say that editOptions is never with the same structure, sometimes not be useful to have the color, just for example sakes. In mongoose one can just define the type to something like: editOptions : {} These editOptions are

How to setState() in React/Apollo with graphQL

不问归期 提交于 2019-12-20 10:57:09
问题 I am trying to setState() to a query result I have from graphQL, but I am having difficulty finding out how to do this because it will always be loading, or it's only used from props. I first set the state constructor (props) { super(props); this.state = { data: [] }; Then I have this query const AllParams = gql` query AllParamsQuery { params { id, param, input } }` And when it comes back I can access it with this.props.AllParamsQuery.params How and when should I this.setState({ data: this

Authentication and Access Control with Relay

杀马特。学长 韩版系。学妹 提交于 2019-12-20 09:36:31
问题 The official line from Facebook is that Relay is "intentionally agnostic about authentication mechanisms." In all the examples in the Relay repository, authentication and access control are a separate concern. In practice, I have not found a simple way to implement this separation. The examples provided in the Relay repository all have root schemas with a viewer field that assumes there is one user. And that user has access to everything. However, in reality, an application has has many users

GraphQL - How to distinguish Public from Private fields?

一曲冷凌霜 提交于 2019-12-19 04:05:41
问题 Context I have a GraphQL API and a NodeJS & Angular application with a MongoDB database that holds users. For each user, there is a public page with public information like id and username . When a user is logged in, there is a private profile page with extended information like an email . Just for context, I'm using jsonwebtoken with accesscontrol to authenticate and authorize a user. The information is stored on the Context of every GraphQL resolve function, so whatever is needed to