简体   繁体   中英

Building Mutation Graphql Query

I am quite new to GraphQL so I am struggling a little bit to understand how to write a proper Query on the front-end.

So, this is the Mutation I've on the server-side

type Mutation {
    addTerminal(terminal: TerminalInput): Terminal
    // other stuff not related
}
type Terminal {
    terminalId: String!
    merchantId: String
    terminalDesignator: String
}

input TerminalInput {
    terminalId: String!
    merchantId: String
    terminalDesignator: String
}

I believe it is using the right structure, but when I try to connect with the client-side im a bit confused.

This is the query I've on the front-end.

export const ADD_TERMINAL_MUTATION = () => (

mutation addTerminalMutation($terminalId: TerminalInput) { addTerminal(terminal: { terminalId: $terminalId }) { terminalId, merchantId, terminalDesignator, } } );

and when I fire it to the server, I receive the following feedback:

Variable "$terminalId" of type "TerminalInput" used in position expecting type "String.".

So I changed to this:

addTerminal(terminal: { terminalId: "123" }) {

and got the error

Variable "$terminalId" is never used in operation "addTerminalMutation".

If I change to

mutation addTerminalMutation($terminalId: String!) {

It says that the TerminalId wasnt provided, but if I log it, it can be seen

在此处输入图像描述

So, what is the right way to write this?

Thanks.

You need to change addTerminalMutation($terminalId: TerminalInput) to addTerminalMutation($terminalId: String!) to indicate the correct type.

First, the

Variable "$terminalId" is never used in operation "addTerminalMutation".

is caused by passing the variable in line

mutation addTerminalMutation($terminalId: TerminalInput)

and never referencing it later.

The problem in the second part seems that the terminalId parameter is not decomposed. The parameter in the line

addTerminal(terminal: { terminalId: $terminalId })

is expected to be a String..

You could do something like this:

addTerminal(input: $terminalId)

to pass in the whole terminal id object.

See this for further info: https://blog.apollographql.com/designing-graphql-mutations-e09de826ed97

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM