Bird
0
0

Which is the correct way to define the input argument and use it in the mutation?

hard📝 Application Q15 of 15
GraphQL - Mutations
You want to create a mutation that accepts a complex input object with fields title (String), content (String), and tags (list of Strings). Which is the correct way to define the input argument and use it in the mutation?
Atype PostInput { title: String!, content: String!, tags: [String!]! }<br>mutation createPost(post: PostInput!) { addPost(post: post) { id } }
Binput PostInput { title: String, content: String, tags: String }<br>mutation createPost($post: PostInput) { addPost(post: $post) { id } }
Cinput PostInput { title: String!, content: String!, tags: [String!]! }<br>mutation createPost($post: PostInput!) { addPost(input: $post) { id } }
Dinput PostInput { title: String!, content: String!, tags: [String] }<br>mutation createPost($post: PostInput!) { addPost(input: post) { id } }
Step-by-Step Solution
Solution:
  1. Step 1: Define input type correctly

    Use input keyword with required fields and list type: tags: [String!]! means list of non-null strings, list itself required.
  2. Step 2: Declare variable and use it properly

    Declare variable with $post: PostInput! and use it as input: $post inside mutation.
  3. Final Answer:

    input PostInput { title: String!, content: String!, tags: [String!]! }
    mutation createPost($post: PostInput!) { addPost(input: $post) { id } }
    -> Option C
  4. Quick Check:

    Use input type and $variable with correct usage [OK]
Quick Trick: Use input type and $variable with correct usage inside mutation [OK]
Common Mistakes:
  • Using type instead of input for input objects
  • Omitting $ in variable declaration or usage
  • Incorrect list type syntax or missing non-null markers
  • Passing variable without $ inside mutation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes