LoginSignup
4
3

こんにちは。

あるGraphQLのスキーマ、とくに引数を取るスキーマに対して、あれどんなデータをPOSTすればいいんだっけ? っていつもわからなくなっちゃうので備忘。

たとえばこんなスキーマを考えます。

type Mutation {
  updateUser(user: UserInput): Response
}

type Response {
  success: Boolean!
}

input UserInput {
  userId: ID!
  firstName: String
  lastName: String
  email: String
  age: Int
  companyCode: String
}

updateUser というMutationは、userという引数 (型はUserInput)をもってて、戻り値はResponse です、というスキーマです。。

書きかた

引数をインラインに展開した場合

このGraphQLに送信するデータは例えば以下のようなデータとなります。

mutation UpdateUser {
  updateUser(user: { userId: "u001", companyCode: "com003" }) {
    __typename
    success
  }
}

こうやって引数のところに直接データを書いちゃうのをインラインに展開した書き方というみたいですね。

ちなみに、こういうデータをGraphQLでPOSTするときのcurlコマンドはこんな感じになります。

$ cat data.txt 
{
  "query":
  "mutation UpdateUser {
    updateUser(user: { userId: \"u001\", companyCode: \"com003\" }) {
      __typename
      success
    }
  }",
  "variables":{}
}

$ cat data.txt | curl --data @-  \
  --request POST \
  --header 'content-type: application/json' \
  --url http://localhost:3000/graphql | jq
  
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   225  100    65  100   160   1887   4645 --:--:-- --:--:-- --:--:--  6818

{
  "data": {
    "updateUser": {
      "__typename": "Response",
      "success": true
    }
  }
}

うまく送信ができたようです。

引数のデータを外出しにした場合

引数をインラインに展開している書き方は、クォートのエスケープとかが必要で、ちょっと微妙です。というわけでその引数のデータを変数にして、外出しして記述する方法もあります。

$ cat data.txt 
{
    "query":
    "mutation UpdateUser($userInput1: UserInput) {
      updateUser(user: $userInput1) {
        __typename
        success
      }
    }",
    "variables":{
        "userInput1":{
            "userId":"u001",
            "companyCode":"com003"
        }
    }
}

userの実データ部分が $userInput1として外だしされていて、下の variablesにデータが移動していますね。

さあcurlしてみましょう。

$  cat data.txt | curl --data @-  \
  --request POST \
  --header 'content-type: application/json' \
  --url http://localhost:3000/graphql | jq

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   328  100    65  100   263   3350  13557 --:--:-- --:--:-- --:--:-- 17263
{
  "data": {
    "updateUser": {
      "__typename": "Response",
      "success": true
    }
  }
}
$

うまくいきました。

おまけ

引数は下記のように、複数セットすることもできます。

$ cat data.txt 
{
    "query":
    "

mutation UpdateUser($userInput1: UserInput, $user: UserInput) {
  updateUser(user: $userInput1) {
    __typename
    success
  }
  createUser(user: $user) {
    id
    company {
      name
      code
    }
  }
}

  ",
  "variables":{
  
  "userInput1": {
    "userId": "u001",
    "companyCode": "com003"
  },
  "user": {
    "userId": "u011",
    "companyCode": "com003"
  }
	
	}
}
cat data.txt | curl --data @-  \
  --request POST \
  --header 'content-type: application/json' \
  --url http://localhost:3000/graphql | jq

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   511  100   138  100   373   3436   9287 --:--:-- --:--:-- --:--:-- 13102
{
  "data": {
    "updateUser": {
      "__typename": "Response",
      "success": true
    },
    "createUser": {
      "id": "u011",
      "company": {
        "name": "会社03",
        "code": "com003"
      }
    }
  }
}
$

おまけ2

もはや引数と関係ないですが、おなじメソッドを別名をつけて複数呼び出すこともできます。

query Users {
  user1:users {
    id
  }
  user2:users {
    id
    companyCode
  }
}
{
  "data": {
    "user1": [
      {
        "id": "u001"
      },
      {
        "id": "u002"
      },...
    ],
    "user2": [
      {
        "id": "u001",
        "companyCode": "com003"
      },
      {
        "id": "u002",
        "companyCode": "com001"
      },...
    ]
  }
}

お疲れさまでした。

関連リンク

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3