Commit ee4ba6ce authored by Bob Van Landuyt's avatar Bob Van Landuyt

Adjust GraphQL helper to query empty fields

These adjustments make sure our GraphQL helpers support rendering
queries for empty fields like this:

      {
        echo(text: "Hello world")
      }

Instead of like this:

     {
       echo(text: "Hello world") {
       }
     }

The latter would be an invalid query, causing parsing errors.
parent 2e154942
...@@ -79,12 +79,21 @@ module GraphqlHelpers ...@@ -79,12 +79,21 @@ module GraphqlHelpers
attributes = attributes_to_graphql(attributes) attributes = attributes_to_graphql(attributes)
attributes = "(#{attributes})" if attributes.present? attributes = "(#{attributes})" if attributes.present?
<<~QUERY <<~QUERY
#{name}#{attributes} { #{name}#{attributes}
#{fields} #{wrap_fields(fields)}
}
QUERY QUERY
end end
def wrap_fields(fields)
return unless fields.strip.present?
<<~FIELDS
{
#{fields}
}
FIELDS
end
def all_graphql_fields_for(class_name, parent_types = Set.new) def all_graphql_fields_for(class_name, parent_types = Set.new)
type = GitlabSchema.types[class_name.to_s] type = GitlabSchema.types[class_name.to_s]
return "" unless type return "" unless type
...@@ -116,8 +125,8 @@ module GraphqlHelpers ...@@ -116,8 +125,8 @@ module GraphqlHelpers
end.join(", ") end.join(", ")
end end
def post_graphql(query, current_user: nil, variables: nil) def post_graphql(query, current_user: nil, variables: nil, headers: {})
post api('/', current_user, version: 'graphql'), params: { query: query, variables: variables } post api('/', current_user, version: 'graphql'), params: { query: query, variables: variables }, headers: headers
end end
def post_graphql_mutation(mutation, current_user: nil) def post_graphql_mutation(mutation, current_user: nil)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment