GraphQL directives with an object array argument - graphql

I am trying to define a GraphQL directive which has an array argument which looks like this:
input Data {
someField: String!
anotherField: String!
}
directive #someDirective( args: [Data!]! ) on OBJECT
type SomeType #someDirective( args: [{someField: "blah", anotherField: "blah"}] )
{
empty: String
}
If I change Data to be a type, the GraphQL compiler get the error:
"The type of #someDirective(args:) must be Input Type but got: [Data!]!."
The problem with using input is that there is no validation of the input by the compiler. This compiles fine:
type AnotherType #someDirective( args: [{blah: "blah", blah2: "blah"}] )
{
empty: String
}
Is it possible to define an object array argument for a directive which gets validated by the compiler?

Related

Resolving nullable fields with directives

I'm developing a GraphQL Directive named bind which can be used on fields to resolve into another values.
# This is a simplification of the actual definitions
directive #bind(value: String) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
input Request {
expr: String! #bind(value: "/v1.0/std/expr")
}
type Response {
message: String
}
type Query {
hello(request: Request): Response
}
It works well if expr is a non-null string. I want my directive to be invoked even if expr is null.
Would you please tell me how?

How to combine multiple field in GraphQL Object

I am trying my first Graphql Schema design. Is it possible that single field inside object type refer to a complex object?
enum KeyPrefix {
WS
WK
SI
ENT
}
input generalKey {
keyPrefix:KeyPrefix!
key:Int!
}
type Item
{
pk: generalKey!
data: String!
name: String!
}
It gives me error as below.
The type of Item.pk must be Output Type but got: generalKey!
input is a keyword reserved for describing input to a GraphQL query. Your schema should look like this:
enum KeyPrefix {
(your values)
}
type PrimaryKey {
prefix: KeyPrefix!
key: Int!
}
type Item {
pk: PrimaryKey!
data: String!
name: String!
}
When you define a Query in your GraphQL schema, you will want to use an input, like so:
input PrimaryKeyInput {
prefix: KeyPrefix!
key: Int!
}
type Query {
getItemByPrimaryKey(input: PrimaryKeyInput!): Item
}
This will allow a client to ask for an Item using the same fields you have on PrimaryKey.

Array of Objects Apollo Server and post from react

So i'm trying to figure out how to pass an array of objects from a POST request to apollo server on AWS lambda.
I've checked this out but it's not the same problem
Array of objects convert into object of objects when I use Apollo
The post request looks like this...
api.post('query', { query : `mutation {saveNewItem(description: "${description}", specials: ${JSON.stringify(specials)}){name}}`})
// comment to get rid of silly scroll bar overlapping code
Schema looks like this...
const { gql } = require('apollo-server-lambda')
const typeDefs = gql`
type ShoppingItem {
description: String
specials: [Specials]
}
input Specials {
description: String
price: String
qty: String
saved: String
}
type Mutation {
saveNewItem(description: String!, specials: [Specials]) : ShoppingItem
}
`
example Specials looks like this...
[{ // Object One
description: "First One"
price: "1.00"
qty: "1"
saved: "false"
},{ // Object two
description: "Second One"
price: "1.00"
qty: "1"
saved: "false"
}]
The error I get currently is...
'Error: The type of ShoppingItem.specials must be Output Type but got: [Specials].',
'at assertValidSchema (/Users/me/Desktop/Projects/app/build/node_modules/graphql/type/validate.js:71:11)',
If I change it to a normal "type" it complains about it not being Input type.
I've also been through the apollo server docs and can't quite see what I'm doing wrong?
Please that as mentioned by Daniel in comments whilst technically the "duplicate" answer given is correct the information offered here is far more high quality and useful to people facing the problem(in my opinion)
You can only use input types for input (GraphQLInputObjectType) and object types for output (GraphQLObjectType). You are using Specials as both: As output type for the field specials in ShoppingItem and as input type in mutation argument specials. To do this you need two types. The reason for this is that output types (can) have resolvers (this is actually abstracted away from apollo server in your case). You will have to create two different types:
type ShoppingItem {
description: String
specials: [Specials]
}
type Specials {
description: String
price: String
qty: String
saved: String
}
input SpecialsDraft {
description: String
price: String
qty: String
saved: String
}
type Mutation {
saveNewItem(description: String!, specials: [SpecialsDraft]) : ShoppingItem
}

GraphQL no object type?

I've read everything, understood no solution and concrete explanation (even here: Apollo / GraphQl - Type must be Input type)
I want to create an object System that contains Suns. So I do:
type System {
_id: ID
name: String! #unique
diameter: Int
suns: [Sun]
}
type Sun {
_id: ID
name: String
diameter: Int
}
type Mutation {
createSystem(name: String!, diameter: Int, suns: [Sun]): System!
}
And I write in playground:
mutation {
createSystem(name:"new system", suns: [{ name: "John" }]) {
name
suns
}
}
But I got a terminal error: "Error: The type of Mutation.createSystem(suns:) must be Input Type but got: [Sun]."
I understand that Sun isn't received as an object. How to declare it as an object?
Thank you very much for your answers
The GraphQL spec. does not allow using type (i.e. output type) as the input argument.It only allows the input arguments to be enum , Scalar and Input . That means you have to create a SunInput
input SunInput {
_id: ID
name: String
diameter: Int
}
You need to make a custom "Type" for sun with its own resolver.
suns: { type: new GraphQLList(SunType) } // just an example
mutation {
createSystem(name:"new system", suns-names: "John") {
name
}
}
It will have resolver that writes a new system to the database called new system that adds a sun of "SunType" to the a database collection with the name of "Sun" for example.

GraphQL mutation variables

I'm trying to do a simple mutation using GraphQL with the GraphiQL interface. My mutation looks like this:
mutation M($name: String) {
addGroup(name:$name) {
id,
name
}
}
with variables:
{
"name": "ben"
}
But it gives me the error: Variable $name of type "String" used in position expecting type "String!"
If I change my mutation to mutation M($name: String = "default") it works as expected. This looks like it's related to the type system, but I can't seem to figure out what the problem is.
You probably defined the input name as a non-null string (something like type: new GraphQLNonNull(GraphQLString) if using js server, or String! in plain GraphQL).
So your input in the mutation must match, which means it must also be a non-null string. If you change to the following, it should work:
mutation M($name: String!) {
addGroup(name:$name) {
id,
name
}
}
Also if you define a default value as you did, it will be a non-null string.
Finally, you could drop the requirement of being a non-null in the server.
I think in you addGroup() mutation the args for name is of type String! that is new GraphQLNonNull(GraphQLString) but in your mutation you specify as String which conflicts with the type system.
When you have an error like this check your database model, in my case I checked my schema and as mongo pluralizes the words was causing me error but I could fix it, also check the documentation.
mutation {
createProject(
name:"project two",
description:"project two"
) {
name
}
}
=> works

Resources