Module: Zenspec::Helpers::GraphQLHelpers

Defined in:
lib/zenspec/helpers/graphql_helpers.rb

Instance Method Summary collapse

Instance Method Details

#graphql_execute(query, variables: {}, context: {}, schema: nil) ⇒ GraphQL::Query::Result

Execute a GraphQL query with variables and context

Examples:

result = graphql_execute(query, variables: { id: "123" }, context: { current_user: user })
expect(result).to succeed_graphql

Parameters:

  • The GraphQL query string

  • (defaults to: {})

    Query variables

  • (defaults to: {})

    Execution context

  • (defaults to: nil)

    Optional schema (defaults to AppSchema)

Returns:

  • The query result



18
19
20
21
22
23
24
25
26
27
# File 'lib/zenspec/helpers/graphql_helpers.rb', line 18

def graphql_execute(query, variables: {}, context: {}, schema: nil)
  schema_class = schema || default_schema
  raise "No GraphQL schema found. Please define AppSchema or pass schema: argument" unless schema_class

  schema_class.execute(
    query,
    variables: variables,
    context: context
  )
end

#graphql_execute_as(user, query, variables: {}, context: {}, schema: nil, context_key: :current_user) ⇒ GraphQL::Query::Result

Execute a GraphQL query with a user automatically added to context

Examples:

result = graphql_execute_as(user, query, variables: { id: "123" })
expect(result).to succeed_graphql

Parameters:

  • The user object to add to context

  • The GraphQL query string

  • (defaults to: {})

    Query variables

  • (defaults to: {})

    Additional context (merged with user context)

  • (defaults to: nil)

    Optional schema (defaults to AppSchema)

  • (defaults to: :current_user)

    The key to use for the user in context (defaults to :current_user)

Returns:

  • The query result



43
44
45
46
# File 'lib/zenspec/helpers/graphql_helpers.rb', line 43

def graphql_execute_as(user, query, variables: {}, context: {}, schema: nil, context_key: :current_user)
  merged_context = context.merge(context_key => user)
  graphql_execute(query, variables: variables, context: merged_context, schema: schema)
end

#graphql_mutate(mutation, input: {}, context: {}, schema: nil) ⇒ GraphQL::Query::Result

Execute a GraphQL mutation with input

Examples:

result = graphql_mutate(mutation, input: { name: "John", email: "[email protected]" })
expect(result).to succeed_graphql

Parameters:

  • The GraphQL mutation string

  • (defaults to: {})

    Mutation input variables

  • (defaults to: {})

    Execution context

  • (defaults to: nil)

    Optional schema (defaults to AppSchema)

Returns:

  • The mutation result



60
61
62
# File 'lib/zenspec/helpers/graphql_helpers.rb', line 60

def graphql_mutate(mutation, input: {}, context: {}, schema: nil)
  graphql_execute(mutation, variables: { input: input }, context: context, schema: schema)
end

#graphql_mutate_as(user, mutation, input: {}, context: {}, schema: nil, context_key: :current_user) ⇒ GraphQL::Query::Result

Execute a GraphQL mutation as a specific user

Examples:

result = graphql_mutate_as(user, mutation, input: { name: "John" })
expect(result).to succeed_graphql

Parameters:

  • The user object to add to context

  • The GraphQL mutation string

  • (defaults to: {})

    Mutation input variables

  • (defaults to: {})

    Additional context (merged with user context)

  • (defaults to: nil)

    Optional schema (defaults to AppSchema)

  • (defaults to: :current_user)

    The key to use for the user in context (defaults to :current_user)

Returns:

  • The mutation result



78
79
80
81
# File 'lib/zenspec/helpers/graphql_helpers.rb', line 78

def graphql_mutate_as(user, mutation, input: {}, context: {}, schema: nil, context_key: :current_user)
  merged_context = context.merge(context_key => user)
  graphql_mutate(mutation, input: input, context: merged_context, schema: schema)
end