Skip to content

add @mock spec#10

Open
magicmark wants to merge 23 commits intographql:mainfrom
magicmark:mock_spec
Open

add @mock spec#10
magicmark wants to merge 23 commits intographql:mainfrom
magicmark:mock_spec

Conversation

@magicmark
Copy link
Copy Markdown
Contributor

@magicmark magicmark commented Mar 8, 2026

@magicmark magicmark changed the title add @mock spec add @mock spec Mar 8, 2026
magicmark and others added 6 commits March 8, 2026 16:17
Mock data is always included in the response regardless of @Skip or
@include directives on the same field.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Adds a value: String argument to @mock that allows inline scalar mock
values directly in the query without needing a mock file. The value and
variant arguments are mutually exclusive.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@rebello95 rebello95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments documenting our discussion earlier today, as well as a few other things I noticed 😄 Thanks for the hard work here!

Comment thread GAP-0/DRAFT.md
Comment thread GAP-0/DRAFT.md
used on operation roots.

If `@mock` is applied to non-root fields only, the client must transform the
document to remove any selections which have `@mock` applied before sending the
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that is probably worth noting here is that for a document to remain valid, the field removal likely needs to happen recursively:

  • Empty selection sets are invalid, so if all fields within a selection set are marked with @mock, the parent field needs to be removed as well. Same goes for fragment definitions and inline fragments
  • Unused variables are invalid, so if a @mocked field is the only field that used an argument, the argument needs to be removed from the root query
  • An operation with no selection sets is invalid, @mocking all top-level fields should be disallowed (in favor of mocking the top-level operation itself)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great points.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these clarifications are still missing; maybe we add them to the next line?

Comment thread GAP-0/DRAFT.md
Comment thread GAP-0/DRAFT.md Outdated
Comment thread GAP-0/DRAFT.md Outdated
Comment on lines +259 to +280
#### __appliesTo__

{"__appliesTo__"} must be defined as the resolved
_[schema coordinate](https://spec.graphql.org/September2025/#sec-Schema-Coordinates)_
of the field or root operation type where `@mock` is applied, or may be applied,
with the *mock variant id*.

**Example**

For the following fragment:

```graphql example
fragment FooFields on Foo {
# location resolves to "Foo.bar"
bar @mock(variant: "basic-bar")

# location resolves to "Foo.baz"
baz @mock(variant: "basic-baz") {
qux
}
}
```
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From our call today:

  • Using schema coordinates here will not work for field aliases (since those don't exist in the schema)
  • We need to know the path for which a given mock is being applied to (not only the type) in order to ensure it provides necessary data for all the required fields (see the example below - user-1 and user-2 variants represent the same type, but both would be invalid for the other's field path)
  • To work around this, we propose changing __appliesTo__ to __path__ and changing its representation to be a path relative to the parent executable fragment/operation
  • For @mock on the root query, __path__ could be . <root>, or something else
query SomeQuery {
  user(id: 123) @mock(variant: 'user-1') { # returns User
    name
  }
  house(id: 123) { # returns User
    owner @mock(variant: 'renter-1') {
      name
      avatarUrl
    }
  }
  somethingElseThatReturnsAUser @mock(variant: 'user-2') { # returns User
    password
  }
}

Copy link
Copy Markdown
Contributor Author

@magicmark magicmark Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option 1

  • @mock on a query root does __path__ = "Query"
  • @mock within queries do __path__ = "Query.bar.baz"
  • @mock within fragments do __path__ = "Foo.bar.baz" (where "Foo" is the name of the fragment)

this feels nice but also possibly redundant, given that the "Query." or "." prefix is already implied by the mock file name.

Option 2

Same as above but remove the "Query." prefix for mocks within an operation/fragment:

  • @mock on a query root does __path__ = "Query"
  • @mock within queries do __path__ = "bar.baz"
  • @mock within fragments do __path__ = "bar.baz"

Given that response positions also do not use a "Query." prefix I guess I'll go with option 2, but happy to bikeshed on this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjie points out that https://github.com/graphql/graphql-wg/blob/main/rfcs/OperationExpressions.md is already a (proposed) thing we could use here instead.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option 2 seems reasonable to me 👍🏽

Comment thread GAP-0/DRAFT.md Outdated
Comment thread GAP-0/DRAFT.md

#### data

:: {"data"} stores the *mock value*. It may be `null` if the field is nullable.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth noting that this data will be automatically inserted within the root data key in the operation's response data. In other words, for a @mock on a root query, its entry would be this:

{
  "mock-id": {
    "data": {
      "business": {
        "name": "The Great British Bakery",
        "rating": 5.0
      }
    },
    ...
  },
}

not this:

{
  "mock-id": {
    "data": {
      "data" {
        "business": {
          "name": "The Great British Bakery",
          "rating": 5.0
        }
      }
    },
    ...
  },
}

Comment thread GAP-0/DRAFT.md Outdated
Comment thread GAP-0/DRAFT.md
Copy link
Copy Markdown

@rebello95 rebello95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on this! I read through the whole thing and mostly have small nits/comments. Overall this is coming together!

Comment thread GAP-0/DRAFT.md Outdated
Comment thread GAP-0/DRAFT.md Outdated
Comment thread GAP-0/DRAFT.md
used on operation roots.

If `@mock` is applied to non-root fields only, the client must transform the
document to remove any selections which have `@mock` applied before sending the
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these clarifications are still missing; maybe we add them to the next line?

Comment thread GAP-0/DRAFT.md Outdated
Comment thread GAP-0/DRAFT.md Outdated
Comment thread GAP-0/SKILL.md Outdated
Comment thread GAP-0/SKILL.md Outdated
Comment thread GAP-0/SKILL.md

The values generated for leaf nodes do not matter and do not need to be
preserved or included in the description - unless otherwise specified by the
user.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest mentioning that aliases should be used in __path__ when present

Comment thread GAP-0/SKILL.md
3. If creating a new mock, create a new entry with a descriptive mock variant id
4. Ensure the mock value includes appropriate `data` and/or `errors` fields
5. Add a summary of the user's prompt to the `__description__` field
6. Write the updated mock file
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
6. Write the updated mock file
6. Write the updated mock file (do not modify other mocks in the file)

Comment thread GAP-0/SKILL.md

## Schema

Look for the GraphQL schema in <repo_root>/schema.graphql to understand what
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Look for the GraphQL schema in <repo_root>/schema.graphql to understand what
Look for the GraphQL schema in <repo_root>/schema.graphqls to understand what

May want to consider removing the filename here since it's implementation-specific

magicmark and others added 9 commits April 4, 2026 01:38
Co-authored-by: Michael Rebello <me@michaelrebello.com>
Co-authored-by: Michael Rebello <me@michaelrebello.com>
Co-authored-by: Michael Rebello <me@michaelrebello.com>
Co-authored-by: Michael Rebello <me@michaelrebello.com>
Co-authored-by: Michael Rebello <me@michaelrebello.com>
Co-authored-by: Michael Rebello <me@michaelrebello.com>
Co-authored-by: Michael Rebello <me@michaelrebello.com>
Co-authored-by: Michael Rebello <me@michaelrebello.com>
Co-authored-by: Michael Rebello <me@michaelrebello.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants