Skip to content

Commit

Permalink
feat: ast input support added (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceres6 authored Jan 3, 2023
1 parent af71653 commit 984480c
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 3 deletions.
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,14 @@ const plugin = fp(async function (app, opts) {
try {
document = parse(source)
} catch (syntaxError) {
const err = new MER_ERR_GQL_VALIDATION()
err.errors = [syntaxError]
throw err
try {
// Try to parse the source as ast
document = JSON.parse(source)
} catch {
const err = new MER_ERR_GQL_VALIDATION()
err.errors = [syntaxError]
throw err
}
}

// Trigger preValidation hook
Expand Down
234 changes: 234 additions & 0 deletions test/app-decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1203,3 +1203,237 @@ test('calling extendSchema throws an error if federationMetadata is enabled', as
t.end()
}
})

test('support ast input', async (t) => {
const app = Fastify()
const schema = `
type Query {
add(x: Int, y: Int): Int
}
`

const resolvers = {
add: async ({ x, y }) => x + y
}

app.register(GQL, {
schema,
resolvers
})

// needed so that graphql is defined
await app.ready()

const query = `{
"kind": "Document",
"definitions": [
{
"kind": "OperationDefinition",
"operation": "query",
"variableDefinitions": [],
"directives": [],
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "add"
},
"arguments": [
{
"kind": "Argument",
"name": {
"kind": "Name",
"value": "x"
},
"value": {
"kind": "IntValue",
"value": "2"
}
},
{
"kind": "Argument",
"name": {
"kind": "Name",
"value": "y"
},
"value": {
"kind": "IntValue",
"value": "2"
}
}
],
"directives": []
}
]
}
}
]
}`
const res = await app.graphql(query)

t.same(res, {
data: {
add: 4
}
})
})

test('throws on invalid ast input', async (t) => {
const app = Fastify()
const schema = `
type Query {
add(x: Int, y: Int): Int
}
`

const resolvers = {
add: async ({ x, y }) => x + y
}

app.register(GQL, {
schema,
resolvers
})

// needed so that graphql is defined
await app.ready()

// missing "kind": "Document",
const query = `{
"definitions": [
{
"kind": "OperationDefinition",
"operation": "query",
"variableDefinitions": [],
"directives": [],
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "add"
},
"arguments": [
{
"kind": "Argument",
"name": {
"kind": "Name",
"value": "x"
},
"value": {
"kind": "IntValue",
"value": "2"
}
},
{
"kind": "Argument",
"name": {
"kind": "Name",
"value": "y"
},
"value": {
"kind": "IntValue",
"value": "2"
}
}
],
"directives": []
}
]
}
}
]
}`
try {
await app.graphql(query)
} catch (err) {
t.equal(err.message, 'Invalid AST Node: { definitions: [[Object]] }.')
t.equal(err.name, 'Error')
}
})

test('support ast input on external requests', async (t) => {
const app = Fastify()
const schema = `
type Query {
add(x: Int, y: Int): Int
}
`

const resolvers = {
add: async ({ x, y }) => x + y
}

app.register(GQL, {
schema,
resolvers
})

// needed so that graphql is defined
await app.ready()

const query = `{
"kind": "Document",
"definitions": [
{
"kind": "OperationDefinition",
"operation": "query",
"variableDefinitions": [],
"directives": [],
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "add"
},
"arguments": [
{
"kind": "Argument",
"name": {
"kind": "Name",
"value": "x"
},
"value": {
"kind": "IntValue",
"value": "2"
}
},
{
"kind": "Argument",
"name": {
"kind": "Name",
"value": "y"
},
"value": {
"kind": "IntValue",
"value": "2"
}
}
],
"directives": []
}
]
}
}
]
}`
const res = await app.inject({
method: 'POST',
url: '/graphql',
body: { query }
})

t.same(JSON.parse(res.body), {
data: {
add: 4
}
})
})

0 comments on commit 984480c

Please sign in to comment.