UNPKG

graphql-ld

Version:
98 lines (97 loc) 3.59 kB
import { Converter as GraphQlToSparqlConverter } from "graphql-to-sparql"; import { ISingularizeVariables } from "graphql-to-sparql/lib/IConvertContext"; import { ExecutionResult } from "graphql/execution/execute"; import { DocumentNode } from "graphql/language"; import { ContextParser, JsonLdContext } from "jsonld-context-parser"; import * as RDF from "@rdfjs/types"; import { Algebra } from "sparqlalgebrajs"; import { Converter as SparqlJsonToTreeConverter } from "sparqljson-to-tree"; import { IQueryEngine } from "./IQueryEngine"; /** * A GraphQL-LD client. * * Typical usage: * ``` * const client = new Client({ context, queryEngine }); * const { data } = await client.query({ query: `{ books { name author { name } } }` }); * ``` */ export declare class Client { private readonly context; private readonly queryEngine; private readonly graphqlToSparqlConverter; private readonly sparqlJsonToTreeConverter; constructor(args: IClientArgs); /** * Execute a GraphQL-LD query. * * There are three ways of invoking this methods: * 1. with a GraphQL query string and optional variables: * `client.query({ query: `{...}`, variables: { varName: 123 } })` * 2. with a parsed GraphQL query and optional variables: * `client.query({ query: gql`{...}`, variables: { varName: 123 } })` * 3. with a SPARQL algebra object and a singularizeVariables object * `client.query({ sparqlAlgebra, singularizeVariables })` * This corresponds to the result of {@link Client#graphQlToSparql}. * * @param {QueryArgs} args Query+variables, or SPARQL algebra+singularize variables. * @return {Promise<ExecutionResult>} A promise resolving to a GraphQL result. */ query(args: QueryArgs): Promise<ExecutionResult>; /** * Convert a GraphQL query to SPARQL algebra and a singularize variables object. * @param {string | DocumentNode} query * @param {{[p: string]: any}} variables * @return {Promise<IGraphQlToSparqlResult>} */ graphQlToSparql({ query, variables }: IQueryArgsRaw): Promise<IGraphQlToSparqlResult>; } export interface IClientArgs { /** * A JSON-LD context. * This may be an object, array, or a string (URL to remote context) */ context: JsonLdContext; /** * A query engine that will be used to execute SPARQL queries. */ queryEngine: IQueryEngine; /** * An optional base IRI. */ baseIRI?: string; /** * An optional data factory for RDF quads and terms. */ dataFactory?: RDF.DataFactory; /** * An optional JSON-LD context parser. * Provide this to override the default context parser options. */ contextParser?: ContextParser; /** * An optional GraphQL to SPARQL converter. * Provide this to override the default converter options. */ graphqlToSparqlConverter?: GraphQlToSparqlConverter; /** * An optional SPARQL-JSON to GraphQL tree converter. * Provide this to override the default converter options. */ sparqlJsonToTreeConverter?: SparqlJsonToTreeConverter; } export type QueryArgs = IQueryArgsRaw | IQueryArgsSparql; export interface IQueryArgsRaw { query: string | DocumentNode; variables?: { [key: string]: any; }; queryEngineOptions?: any; } export interface IQueryArgsSparql extends IGraphQlToSparqlResult { queryEngineOptions?: any; } export interface IGraphQlToSparqlResult { sparqlAlgebra: Algebra.Operation; singularizeVariables: ISingularizeVariables; }