I am using graphql-tag package for graphql syntax. The syntax below I can’t understand if it is a valid javascript code.
How does it work? (Just surface level knowledge not the whole package’s working)
import gql from 'graphql-tag';
const query = gql`
{
user(id: 5) {
firstName
lastName
}
}
Below is the implementation of the gql
from the package.
function gql(/* arguments */) {
var args = Array.prototype.slice.call(arguments);
var literals = args[0];
// We always get literals[0] and then matching post literals for each arg given
var result = literals[0];
for (var i = 1; i < args.length; i++) {
if (args[i] && args[i].kind && args[i].kind === 'Document') {
result += args[i].loc.source.body;
} else {
result += args[i];
}
result += literals[i];
}
return parseDocument(result);
}
It seems it is a function. A function needs arguments that we pass inside parenthesis. But, gql
doesn’t have any? How So?