Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ast input support added #935

Merged
merged 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
})
})