Skip to content

Commit

Permalink
chore: add support for un-named definations
Browse files Browse the repository at this point in the history
* fixed 2 cases with unnamed operation and query

* fixed comments

* chore: format files and minor updates

---------

Co-authored-by: Rishabh Chawla <[email protected]>
  • Loading branch information
desout and rishabh3112 authored Jun 20, 2024
1 parent f5d8831 commit effac58
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import gql from 'graphql-tag';

const foo = gql`{foo}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import gql from 'graphql-tag';
const foo = {
"kind": "Document",
"definitions": [
{
"kind": "OperationDefinition",
"directives": [],
"variableDefinitions": [],
"operation": "query",
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "foo"
},
"arguments": [],
"directives": []
}
]
}
}
],
"loc": {
"start": 0,
"end": 5,
"source": {
"body": "{foo}"
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import gql from 'graphql-tag';
const foo = {
"kind": "Document",
"definitions": [
{
"kind": "OperationDefinition",
"directives": [],
"variableDefinitions": [],
"operation": "query",
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "foo"
},
"arguments": [],
"directives": []
}
]
}
}
],
"loc": {
"start": 0,
"end": 5,
"source": {
"body": "{foo}"
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import gql from 'graphql-tag';

const foo = gql`query($foo: String!) {foo1}`;
55 changes: 55 additions & 0 deletions tests/graphql_tag/definations/operation/unnamed_query/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import gql from 'graphql-tag';
const foo = {
"kind": "Document",
"definitions": [
{
"kind": "OperationDefinition",
"directives": [],
"variableDefinitions": [
{
"kind": "VariableDefinition",
"directives": [],
"variable": {
"kind": "Variable",
"name": {
"kind": "Name",
"value": "foo"
}
},
"type": {
"kind": "NonNullType",
"type": {
"kind": "NamedType",
"name": {
"kind": "Name",
"value": "String"
}
}
}
}
],
"operation": "query",
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "foo1"
},
"arguments": [],
"directives": []
}
]
}
}
],
"loc": {
"start": 0,
"end": 27,
"source": {
"body": "query($foo: String!) {foo1}"
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import gql from 'graphql-tag';
const foo = {
"kind": "Document",
"definitions": [
{
"kind": "OperationDefinition",
"directives": [],
"variableDefinitions": [
{
"kind": "VariableDefinition",
"directives": [],
"variable": {
"kind": "Variable",
"name": {
"kind": "Name",
"value": "foo"
}
},
"type": {
"kind": "NonNullType",
"type": {
"kind": "NamedType",
"name": {
"kind": "Name",
"value": "String"
}
}
}
}
],
"operation": "query",
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"kind": "Field",
"name": {
"kind": "Name",
"value": "foo1"
},
"arguments": [],
"directives": []
}
]
}
}
],
"loc": {
"start": 0,
"end": 25,
"source": {
"body": "query($foo:String!){foo1}"
}
}
};
13 changes: 11 additions & 2 deletions transforms/graphql_tag/src/parser/nodes/definitions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ mod operation;
use fragment::create_fragment_definition;
use operation::create_operation_definition;

pub fn create_definition(definition: Definition, span: Span) -> Option<ExprOrSpread> {
pub fn create_definition(
definition: Definition,
span: Span,
assert_definition_name: bool,
) -> Option<ExprOrSpread> {
if assert_definition_name {
definition.name().expect("GraphQL query must have name.");
}
let def_expr = match definition {
Definition::FragmentDefinition(frag_def) => create_fragment_definition(frag_def, span),
Definition::OperationDefinition(operation_def) => {
Expand Down Expand Up @@ -42,8 +49,10 @@ pub fn create_definition(definition: Definition, span: Span) -> Option<ExprOrSpr

pub fn create_definitions(definitions: CstChildren<Definition>, span: Span) -> Expr {
let mut all_definitions = vec![];
let is_multiple_definitions = definitions.clone().count() > 1;

for def in definitions {
all_definitions.push(create_definition(def, span));
all_definitions.push(create_definition(def, span, is_multiple_definitions));
}

Expr::Array(ArrayLit {
Expand Down
17 changes: 12 additions & 5 deletions transforms/graphql_tag/src/parser/nodes/definitions/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use crate::parser::{

pub fn create_operation_definition(definition: OperationDefinition, span: Span) -> Box<Expr> {
let kind = get_key_value_node("kind".into(), "OperationDefinition".into());
let name = get_key_value_node(
"name".into(),
create_name(definition.name().unwrap().text().as_str().into(), span),
);

let variable_definitions = get_key_value_node(
"variableDefinitions".into(),
create_variable_definitions(definition.variable_definitions(), span),
Expand All @@ -34,9 +31,19 @@ pub fn create_operation_definition(definition: OperationDefinition, span: Span)

let mut opr_def = ObjectLit {
span,
props: vec![kind, name, directives, variable_definitions, operation],
props: vec![kind, directives, variable_definitions, operation],
};

if definition.name().is_some() {
opr_def.props.insert(
1,
get_key_value_node(
"name".into(),
create_name(definition.name().unwrap().text().as_str().into(), span),
),
);
}

if definition.selection_set().is_some() {
let selection_set = get_key_value_node(
"selectionSet".into(),
Expand Down
4 changes: 4 additions & 0 deletions transforms/graphql_tag/src/parser/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub fn get_key_value_node(key: String, value: Expr) -> PropOrSpread {
}

pub fn get_operation_token(operation_type: Option<OperationType>) -> String {
if operation_type.is_none() {
return "query".into();
}

let opr_token = operation_type.unwrap();

if opr_token.query_token().is_some() {
Expand Down

0 comments on commit effac58

Please sign in to comment.