Skip to content

Commit

Permalink
enable fragments on top level query/mutation (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
olirice authored Dec 2, 2022
1 parent 23b6f9a commit bbf729d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::omit::*;
use crate::parser_util::*;
use crate::transpile::{MutationEntrypoint, QueryEntrypoint};
use graphql_parser::query::{
Definition, Document, FragmentDefinition, Mutation, OperationDefinition, Query, Selection,
SelectionSet, Text,
Definition, Document, FragmentDefinition, Mutation, OperationDefinition, Query, SelectionSet,
Text,
};
use itertools::Itertools;
use serde_json::{json, Value};
Expand Down Expand Up @@ -138,15 +138,21 @@ where
let query_type = schema_type.query_type();
let map = query_type.field_map();

let selections: Vec<graphql_parser::query::Field<T>> = selection_set
.items
.into_iter()
.filter_map(|def| match def {
Selection::Field(field) => Some(field),
// TODO, handle fragments
_ => panic!("only Selections are supported"),
})
.collect();
let selections = match normalize_selection_set(
&selection_set,
&fragment_definitions,
&query_type.name().unwrap(),
) {
Ok(selections) => selections,
Err(err) => {
return GraphQLResponse {
data: Omit::Omitted,
errors: Omit::Present(vec![ErrorMessage {
message: err.to_string(),
}]),
}
}
};

match selections[..] {
[] => GraphQLResponse {
Expand Down Expand Up @@ -319,15 +325,21 @@ where

let map = mutation_type.field_map();

let selections: Vec<graphql_parser::query::Field<T>> = selection_set
.items
.into_iter()
.filter_map(|def| match def {
Selection::Field(field) => Some(field),
// TODO, handle fragments
_ => panic!("only Selections are supported"),
})
.collect();
let selections = match normalize_selection_set(
&selection_set,
&fragment_definitions,
&mutation_type.name().unwrap(),
) {
Ok(selections) => selections,
Err(err) => {
return GraphQLResponse {
data: Omit::Omitted,
errors: Omit::Present(vec![ErrorMessage {
message: err.to_string(),
}]),
}
}
};

use pgx::prelude::*;
use pgx_contrib_spiext::subtxn::*;
Expand Down
34 changes: 34 additions & 0 deletions test/expected/fragment_on_mutation.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
begin;
create table blog_post(
id int primary key,
title text not null
);
select graphql.resolve($$
mutation {
...blogPosts_insert
}

fragment blogPosts_insert on Mutation {
insertIntoBlogPostCollection(objects: [
{ id: 1, title: "foo" }
]) {
affectedCount
records {
id
title
}
}
}
$$);
resolve
----------------------------------------------------------------------------------------------------------
{"data": {"insertIntoBlogPostCollection": {"records": [{"id": 1, "title": "foo"}], "affectedCount": 1}}}
(1 row)

select * from blog_post;
id | title
----+-------
1 | foo
(1 row)

rollback;
28 changes: 28 additions & 0 deletions test/expected/fragment_on_query.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
begin;
create table blog_post(
id int primary key,
title text not null
);
select graphql.resolve($$
query {
...blogPosts_query
}

fragment blogPosts_query on Query {
blogPostCollection(first:2) {
edges
{
node {
id
title
}
}
}
}
$$);
resolve
-------------------------------------------------
{"data": {"blogPostCollection": {"edges": []}}}
(1 row)

rollback;
28 changes: 28 additions & 0 deletions test/sql/fragment_on_mutation.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
begin;

create table blog_post(
id int primary key,
title text not null
);

select graphql.resolve($$
mutation {
...blogPosts_insert
}

fragment blogPosts_insert on Mutation {
insertIntoBlogPostCollection(objects: [
{ id: 1, title: "foo" }
]) {
affectedCount
records {
id
title
}
}
}
$$);

select * from blog_post;

rollback;
26 changes: 26 additions & 0 deletions test/sql/fragment_on_query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
begin;

create table blog_post(
id int primary key,
title text not null
);

select graphql.resolve($$
query {
...blogPosts_query
}

fragment blogPosts_query on Query {
blogPostCollection(first:2) {
edges
{
node {
id
title
}
}
}
}
$$);

rollback;

0 comments on commit bbf729d

Please sign in to comment.