How to execute GraphQL query from server

前端 未结 2 563
温柔的废话
温柔的废话 2021-02-07 13:06

I am using graphql-express to create an endpoint where I can execute graphql queries in. Although I am using Sequelize with a SQL database it feels wrong to use it directly from

2条回答
  •  迷失自我
    2021-02-07 14:13

    I would like to complete the answer from @aᴍɪʀ by providing the pattern for properly doing a query / mutation with parameters:

    const params = {
      username: 'john',
      password: 'hello, world!',
      userData: {
        ...
      }
    }
    
    query(`mutation createUser(
              $username: String!,
              $password: String!,
              $userData: UserInput) {
      createUserWithPassword(
        username: $username,
        password: $password,
        userData: $userData) {
        id
        name {
          familyName
          givenName
        }
      }
    }`, params)
    

    This way, you don't have to deal with the string construction bits " or ' here and there.

提交回复
热议问题