Skip to content

Commit

Permalink
fix: Required fields throw errors in 'buildQuery'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdao committed Feb 1, 2018
1 parent 580c303 commit e9d3133
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/graphqls2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -1125,9 +1125,13 @@ var _graphQlQueryTypes = { 'query': 'Query', 'mutation': 'Mutation', 'subscripti
variables: ast.variableDefinitions ? ast.variableDefinitions.map(function (_ref3) {
var v = _ref3.variable,
t = _ref3.type;

var nonNullType = t.kind == 'NonNullType';
var exclPoint = nonNullType ? '!' : '';
var typ = nonNullType ? t.type : t;
return {
name: v.name.value,
type: t.kind == 'ListType' ? '[' + t.type.name.value + ']' : t.name.value };
type: typ.kind == 'ListType' ? '[' + typ.type.name.value + ']' + exclPoint : '' + typ.name.value + exclPoint };
}) : null,
properties: parseProperties(ast.selectionSet),
fragments: parseFragments(fragments)
Expand Down
2 changes: 1 addition & 1 deletion lib/graphqls2s.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/graphqls2s.min.js

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,14 @@ const getQueryAST = (query, operationName, schemaAST, options={}) => {
type: ast.operation,
name: ast.name ? ast.name.value : null,
variables: ast.variableDefinitions
? ast.variableDefinitions.map(({ variable:v, type:t }) => ({
name: v.name.value,
type: t.kind == 'ListType' ? `[${t.type.name.value}]` : t.name.value }))
? ast.variableDefinitions.map(({ variable:v, type:t }) => {
const nonNullType = t.kind == 'NonNullType'
const exclPoint = nonNullType ? '!' : ''
const typ = nonNullType ? t.type : t
return {
name: v.name.value,
type: typ.kind == 'ListType' ? `[${typ.type.name.value}]${exclPoint}` : `${typ.name.value}${exclPoint}` }
})
: null,
properties: parseProperties(ast.selectionSet),
fragments: parseFragments(fragments)
Expand Down
36 changes: 36 additions & 0 deletions test/browser/graphqls2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,42 @@ union Details = PriceDetails | RacketDetails
assert.equal(queryAnswer_introspec, query_introspec, 'The rebuild introspec query for the schema request should match the original with fragments.')
assert.equal(queryAnswer_test, query_test, 'The rebuild test query for the schema request should match the original with fragments.')
})))

var schema_8790hdhke3 = `
type Message {
message: String
}
input CredsInput {
token: String!
password: String!
}
type Mutation {
resetPasswordMutation(creds: CredsInput): Message
}
`

var query_8790hdhke3 = `
mutation resetPasswordMutation($token: String!, $password: String!) {
userResetPassword(creds: {token: $token, password: $password}) {
message
__typename
}
}
`

/*eslint-disable */
describe('graphqls2s', () =>
describe('#buildQuery: SUPPORT NON-NULLABLE FIELDS', () =>
it('Should support queries with multiple queries.', () => {
/*eslint-enable */
var schemaAST = getSchemaAST(schema_8790hdhke3)
var queryOpAST = getQueryAST(query_8790hdhke3, null, schemaAST, { defrag: true })
var rebuiltQuery = buildQuery(queryOpAST)

assert.equal(normalizeString(rebuiltQuery), normalizeString(query_8790hdhke3))
})))
}

/*eslint-disable */
Expand Down

0 comments on commit e9d3133

Please sign in to comment.