├── LICENSE ├── README.md ├── imports ├── deps.json ├── gql.ts └── path.ts ├── mod.ts └── types.graphql /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Crew Dev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IMPORTQL 🎡 2 | 3 | easy way to import and parse graphql files in [deno](https://deno.land/) 4 | 5 | ![importql](https://i.ibb.co/k5P665d/importql.png) 6 | 7 | ## install 8 | 9 | install using [Trex](https://deno.land/x/trex) package manager 10 | 11 | ```sh 12 | $ trex install --map importql 13 | ``` 14 | 15 | or use url 16 | 17 | ```javascript 18 | import { importQL } from "https://deno.land/x/importql/mod.ts"; 19 | ``` 20 | 21 | ## use 22 | 23 | ```graphql 24 | # query.graphql file 25 | 26 | type User { 27 | firstName: String 28 | lastName: String 29 | } 30 | 31 | input UserInput { 32 | firstName: String 33 | lastName: String 34 | } 35 | 36 | type ResolveType { 37 | done: Boolean 38 | } 39 | 40 | type Query { 41 | getUser(id: String): User 42 | } 43 | 44 | type Mutation { 45 | setUser(input: UserInput!): ResolveType! 46 | } 47 | ``` 48 | 49 | ```javascript 50 | import { importQL } from "https://deno.land/x/importql/mod.ts"; 51 | 52 | const query = importQL("query.graphql"); // import the .graphql file 53 | 54 | // query is now a GraphQL syntax tree object. 55 | 56 | // { 57 | // "kind": "Document", 58 | // "definitions": [ 59 | // { 60 | // "kind": "OperationDefinition", 61 | // "operation": "query", 62 | // "name": null, 63 | // "variableDefinitions": null, 64 | // "directives": [], 65 | // "selectionSet": { 66 | // "kind": "SelectionSet", 67 | // "selections": [ 68 | // { 69 | // "kind": "Field", 70 | // "alias": null, 71 | // "name": { 72 | // "kind": "Name", 73 | // "value": "user", 74 | // ... 75 | ``` 76 | 77 | ## permissions 78 | 79 | - --allow-read 80 | -------------------------------------------------------------------------------- /imports/deps.json: -------------------------------------------------------------------------------- 1 | { 2 | "meta": { 3 | "gql": { 4 | "url": "https://cdn.skypack.dev/graphql-tag@2.10.1", 5 | "hash": "c7a4e598d237cd8cc2c5d1eb7eb4df67eb0a5e86c34fabf61b9b45e33df3e726" 6 | }, 7 | "path": { 8 | "url": "https://deno.land/std/path/mod.ts", 9 | "hash": "7785d20e2d17f5b217f1f5120bc47adcde3854060e7be740bb8bd539c083020f" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /imports/gql.ts: -------------------------------------------------------------------------------- 1 | import gql from "https://cdn.skypack.dev/graphql-tag@2.10.1"; 2 | 3 | export { gql }; 4 | -------------------------------------------------------------------------------- /imports/path.ts: -------------------------------------------------------------------------------- 1 | export * from "https://deno.land/std/path/mod.ts"; -------------------------------------------------------------------------------- /mod.ts: -------------------------------------------------------------------------------- 1 | import { join } from "./imports/path.ts"; 2 | import { gql } from "./imports/gql.ts"; 3 | 4 | const { readFileSync } = Deno; 5 | /** 6 | * get and parse graphql file. 7 | * @param {string} path - graphql file path. 8 | * @return {object} graphql object. 9 | **/ 10 | export function importQL(path: string): object { 11 | if (!path.includes(".graphql")) { 12 | path = path + ".graphql"; 13 | } 14 | 15 | try { 16 | const decoder = new TextDecoder("utf-8"); 17 | const file = readFileSync(join(Deno.cwd(), path)); 18 | // @ts-ignore 19 | return gql` 20 | ${decoder.decode(file)} 21 | `; 22 | } catch (error) { 23 | console.error(new Error(`error parsing file [${path}]`)); 24 | return {}; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /types.graphql: -------------------------------------------------------------------------------- 1 | type User { 2 | firstName: String 3 | lastName: String 4 | } 5 | 6 | input UserInput { 7 | firstName: String 8 | lastName: String 9 | } 10 | 11 | type ResolveType { 12 | done: Boolean 13 | } 14 | 15 | type Query { 16 | getUser(id: String): User 17 | } 18 | 19 | type Mutation { 20 | setUser(input: UserInput!): ResolveType! 21 | } 22 | --------------------------------------------------------------------------------