How to use @webiny/api - 10 common examples

To help you get started, we’ve selected a few @webiny/api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github webiny / webiny-js / packages / api-forms / src / plugins / graphql / formSubmissionResolvers / exportFormSubmissions.js View on Github external
rows.push(row);
    }

    // Save CSV file and return its URL to the client.
    const csv = await parseAsync(rows, { fields: Object.values(fields) });
    const buffer = Buffer.from(csv);
    const result = await uploadFile({
        context,
        buffer,
        file: {
            size: buffer.length,
            name: "form_submissions_export.csv",
            type: "text/csv"
        }
    });
    return new Response(result);
};
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / pageResolvers / getPublishedPage.js View on Github external
export default async (root: any, args: Object, context: Object) => {
    if (!args.parent && !args.url) {
        return new NotFoundResponse("Page parent or URL missing.");
    }

    // We utilize the same query used for listing published pages (single source of truth = less maintenance).
    const [page] = await listPublishedPages({ context, args: { ...args, perPage: 1 } });

    if (!page) {
        return new NotFoundResponse("The requested page was not found.");
    }

    return new Response(page);
};
github webiny / webiny-js / packages / api-forms / src / plugins / graphql / formSubmissionResolvers / exportFormSubmissions.js View on Github external
let submissions = [];

    const { FormSubmission } = context.models;
    if (ids) {
        submissions = await FormSubmission.find({ query: { id: { $in: args.ids } } });
    } else {
        if (form) {
            submissions = await FormSubmission.find({ query: { "form.revision": form } });
        } else if (parent) {
            submissions = await FormSubmission.find({ query: { "form.parent": parent } });
        }
    }

    // Take first submission, get parent Form entity, and get all distinct fields through all revisions.
    if (submissions.length === 0) {
        return new NotFoundResponse("No form submissions found.");
    }

    const parentForm = await submissions[0].form.parent;
    const revisions = await parentForm.revisions;

    const rows = [];
    const fields = {};

    // First extract all distinct fields across all form submissions.
    for (let i = 0; i < revisions.length; i++) {
        let revision = revisions[i];
        for (let j = 0; j < revision.fields.length; j++) {
            let field = revision.fields[j];
            if (!fields[field.fieldId]) {
                fields[field.fieldId] = field.label.value;
            }
github webiny / webiny-js / packages / api-forms / src / plugins / graphql / formResolvers / getPublishedForm.js View on Github external
export default async (root: any, args: Object, context: Object) => {
    if (!args.id && !args.parent && !args.slug) {
        return new NotFoundResponse("Form ID or slug missing.");
    }

    // We utilize the same query used for listing published forms (single source of truth = less maintenance).
    const listArgs = { ...args, perPage: 1 };
    if (!listArgs.version) {
        listArgs.sort = { version: -1 };
    }

    const plugin = getListPublishedFormsResolver(context);

    const { forms, totalCount } = await plugin.resolve({ root, args: listArgs, context });

    if (!Array.isArray(forms) || !Number.isInteger(totalCount)) {
        throw Error(
            `Resolver plugin "forms-resolver-list-published-forms" must return { forms: [Form], totalCount: Int }!`
        );
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / pageResolvers / getPublishedPage.js View on Github external
export default async (root: any, args: Object, context: Object) => {
    if (!args.parent && !args.url) {
        return new NotFoundResponse("Page parent or URL missing.");
    }

    // We utilize the same query used for listing published pages (single source of truth = less maintenance).
    const [page] = await listPublishedPages({ context, args: { ...args, perPage: 1 } });

    if (!page) {
        return new NotFoundResponse("The requested page was not found.");
    }

    return new Response(page);
};
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / pageResolvers / getPublishedPage.js View on Github external
export default async (root: any, args: Object, context: Object) => {
    if (!args.parent && !args.url) {
        return new NotFoundResponse("Page parent or URL missing.");
    }

    // We utilize the same query used for listing published pages (single source of truth = less maintenance).
    const [page] = await listPublishedPages({ context, args: { ...args, perPage: 1 } });

    if (!page) {
        return new NotFoundResponse("The requested page was not found.");
    }

    return new Response(page);
};
github webiny / webiny-js / packages / api-cms / src / plugins / fieldTypes / ref.js View on Github external
});
                }

                // Load "many" entries
                const { where = {}, ...rest } = args;
                where["id_in"] = refValue || [];

                try {
                    const { entries, meta } = await findEntries({
                        model: refModel,
                        args: { where, ...rest },
                        context,
                        info
                    });

                    return new ListResponse(entries, meta);
                } catch (err) {
                    return new ListErrorResponse({
                        code: err.code,
                        error: err.message
                    });
                }
            };
        }
github webiny / webiny-js / examples / functions / code / api-service-security / src / handler.js View on Github external
// @flow
import { createHandler, PluginsContainer } from "@webiny/api";
import createConfig from "service-config";
import servicePlugins from "@webiny/api/plugins/service";
import securityPlugins from "@webiny/api-security/plugins";

const plugins = new PluginsContainer([servicePlugins, securityPlugins]);

let apolloHandler;

export const handler = async (event: Object, context: Object) => {
    if (!apolloHandler) {
        const config = await createConfig();
        const { handler } = await createHandler({ plugins, config });
        apolloHandler = handler;
    }

    return apolloHandler(event, context);
};
github webiny / webiny-js / examples / functions / code / api-service-files / src / handler.js View on Github external
// @flow
import { createHandler, PluginsContainer } from "@webiny/api";
import createConfig from "service-config";

import servicePlugins from "@webiny/api/plugins/service";
import securityPlugins from "@webiny/api-security/plugins/service";
import filesPlugins from "@webiny/api-files/plugins";

const plugins = new PluginsContainer([servicePlugins, securityPlugins, filesPlugins]);

let apolloHandler;

export const handler = async (event: Object, context: Object) => {
    if (!apolloHandler) {
        const config = await createConfig();
        const { handler } = await createHandler({ plugins, config });
        apolloHandler = handler;
    }

    return apolloHandler(event, context);
};
github webiny / webiny-js / examples / functions / api-service-headless / src / handler.js View on Github external
// @flow
import { createHandler, PluginsContainer } from "@webiny/api";
import servicePlugins from "@webiny/api/plugins/service";
import securityPlugins from "@webiny/api-security/plugins/service";
import headlessPlugins from "@webiny/api-headless/plugins";
import createConfig from "service-config";

const plugins = new PluginsContainer([servicePlugins, securityPlugins, headlessPlugins]);

export const handler = async (context: Object) => {
    const config = await createConfig(context);
    return await createHandler({ config, plugins });
};