---
cases:
  - note: graphql_is_valid/success extending non-existent types
    query: data.test.p = x
    modules:
      - |
        package test

        schema := `
          extend type User {
              id: ID!
          }
          extend type Product {
              upc: String!
          }
          union _Entity = Product | User
          extend type Query {
            entity: _Entity
          }
        `

        query := `
          {
            entity {
              ... on User {
              id
              }
            }
          }
        `

        p if {
        	graphql.is_valid(query, schema)
        }
    want_result:
      - x: true
  - note: graphql_is_valid/success validation rules are independent case 1
    query: data.test.p = x
    modules:
      - |
        package test

        schema := `
          extend type Query {
              myAction(myEnum: Locale!): SomeResult!
          }
          type SomeResult {
              id: String
          }
          enum Locale {
              EN
              LT
              DE
          }
        `

        query := `
          query SomeOperation {
            # Note: Not providing mandatory parameter: (myEnum: Locale!)
            myAction {
              id
            }
          }
        `

        # We use the unification style from semver's is_valid tests here:
        p := x if {
        	x = graphql.is_valid(query, schema)
        }
    want_result:
      - x: false
  - note: graphql_is_valid/success validation rules are independent case 2
    query: data.test.p = x
    modules:
      - |
        package test

        schema := `
          extend type Query {
              myAction(myEnum: Locale!): SomeResult!
          }
          type SomeResult {
              id: String
          }
          enum Locale {
              EN
              LT
              DE
          }
        `

        query := `
          # Note: there is default enum value in variables
          query SomeOperation ($locale: Locale! = DE) {
            myAction(myEnum: $locale) {
              id
            }
          }
        `

        p if {
        	graphql.is_valid(query, schema)
        }
    want_result:
      - x: true
  - note: graphql_is_valid/success deprecating types
    query: data.test.p = x
    modules:
      - |
        package test

        schema := `
          type DeprecatedType {
            deprecatedField: String @deprecated
            newField(deprecatedArg: Int): Boolean
          }
          enum DeprecatedEnum {
            ALPHA @deprecated
          }
        `

        query := ``

        p if {
        	graphql.is_valid(query, schema)
        }
    want_result:
      - x: true
  - note: graphql_is_valid/success no unused variables
    query: data.test.p = x
    modules:
      - |
        package test

        schema := `
          type Query {
            bar: String!
          }
        `

        query := `
          query Foo($flag: Boolean!) {
            ...Bar
          }
          fragment Bar on Query {
            bar @include(if: $flag)
          }
        `

        p if {
        	graphql.is_valid(query, schema)
        }
    want_result:
      - x: true
  - note: graphql_is_valid/success - AST objects - Employee example
    query: data.test.p = x
    modules:
      - |
        package test

        schema_ast := graphql.parse_schema(`
          type Employee {
            id: String!
            salary: Int!
          }

          schema {
            query: Query
          }

          type Query {
            employeeByID(id: String): Employee
          }
        `)

        query_ast := graphql.parse_query(`
          query { employeeByID(id: "alice") { salary }}
        `)

        p if {
        	graphql.is_valid(query_ast, schema_ast)
        }
    want_result:
      - x: true
  - note: graphql_is_valid/undefined argument
    query: data.test.p = x
    modules:
      - |
        package test

        schema := `
          type Query {
            bar: String!
          }
          type Mutation {
            x: String!
          }
        `

        query := `mutation{x(a:b)}`

        p if {
          not graphql.is_valid(query, schema)
        }
    want_result:
      - x: true

  - note: graphql_is_valid/undefined argument schema ast
    query: data.test.p = x
    modules:
      - |
        package test

        schema_ast := {
          "Definitions": [
            {
              "BuiltIn": false,
              "Description": "",
              "Fields": [
                {
                  "Description": "",
                  "Name": "bar",
                  "Type": {
                    "NamedType": "String",
                    "NonNull": true
                  }
                }
              ],
              "Kind": "OBJECT",
              "Name": "Query"
            },
            {
              "BuiltIn": false,
              "Description": "",
              "Fields": [
                {
                  "Description": "",
                  "Name": "x",
                  "Type": {
                    "NamedType": "String",
                    "NonNull": true
                  }
                }
              ],
              "Kind": "OBJECT",
              "Name": "Mutation"
            }
          ]
        }

        query_ast := {
          "Operations": [
            {
              "Name": "",
              "Operation": "mutation",
              "SelectionSet": [
                {
                  "Alias": "x",
                  "Arguments": [
                    {
                      "Name": "a",
                      "Value": {
                        "Kind": 7,
                        "Raw": "b"
                      }
                    }
                  ],
                  "Name": "x"
                }
              ]
            }
          ]
        }

        p if {
          not graphql.is_valid(query_ast, schema_ast)
        }
    want_result:
      - x: true
