Skip to content

Instantly share code, notes, and snippets.

View iliran11's full-sized avatar
🎯
Focusing

Liran Cohen iliran11

🎯
Focusing
View GitHub Profile
@iliran11
iliran11 / server.js
Created May 11, 2020 13:52
Init i18next module
const express = require("express");
const { ApolloServer, gql } = require("apollo-server-express");
const cookieParser = require("cookie-parser");
const intlDirective = require("./intlDirective");
// init i18next module.
require("./i18next");
...
...
@iliran11
iliran11 / server.js
Last active May 4, 2020 05:07
Wire the directive into Apollo server.
// Importing the directive
const intlDirective = require("./intlDirective");
const typeDefs = gql`
directive @intl on FIELD_DEFINITION
type Query {
// declaring we want to use the directive
# Makring "greeting" field for internationalization.
greeting: String @intl
id: Int
}
@iliran11
iliran11 / IntlDirective.js
Last active May 11, 2020 13:55
INTL directive - graphql
const { SchemaDirectiveVisitor } = require("graphql-tools");
const { defaultFieldResolver } = require("graphql");
const i18next = require("i18next");
class IntlDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async (...args) => {
const context = args[2];
const info = args[3];
const value = await resolve.apply(this, args);
@iliran11
iliran11 / server.js
Created May 3, 2020 17:30
final context function - put the t function on the graphql context.
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req }) => {
// now we have the user requeted language
const t = await i18Next("es");
return { t };
},
});
@iliran11
iliran11 / i18nextManager.js
Last active May 11, 2020 13:25
i18NextManager gets a requested language, and returns the t function
const i18next = require("i18next");
i18next.init({
lng,
resources: {
en: {
translation: {
greeting_key: "hello",
},
},
es: {
@iliran11
iliran11 / server.js
Last active May 11, 2020 14:00
apollo context with reading the lang cookie
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req }) => {
// now we have the user requeted language
const lng = req.cookies.lng;
return { lng };
},
});
@iliran11
iliran11 / server.js
Last active May 4, 2020 04:58
internationalize graphql
const express = require("express");
const { ApolloServer, gql } = require("apollo-server-express");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
greeting: String
id: Int
@iliran11
iliran11 / query-with-intl-directive
Last active May 4, 2020 04:54
Internationalize your Graphql - server.js
const typeDefs = gql`
directive @intl on FIELD_DEFINITION
type Query {
# Makring "greeting" field for internationalization.
greeting: String @intl
id: Int
}
`;
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
<circle style="fill: #32cbd6;" cx="256" cy="267" r="245"/>
<path style="fill:#D9EAFC;" d="M61.914,416.511C106.715,474.582,176.982,512,256,512c47.739,0,92.279-13.668,129.948-37.284V134.345 H61.914V416.511z"/>
<path style="fill:#C3DDF4;" d="M81.756,134.345H61.914v282.166c6.144,7.964,12.783,15.524,19.842,22.668V134.345z"/>
<rect x="61.914" y="35.902" style="fill:#02AF8E;" width="324.03" height="99.44"/>
<rect x="61.914" y="35.902" style="fill:#0F9390;" width="19.842" height="99.44"/>
<path style="fill:#1A2B63;" d="M376.391,0H71.472c-5.257,0-9.558,4.301-9.558,9.558v27.344h324.034V9.558 C385.948,4.301,381.647,0,376.391,0z"/>
<path style="fill:#52BBEF;" d="M347.98,295.832c0,1.965-1.608,3.573-3.572,3.573H201.505c-1.965,0-3.572-1.608-3.572-3.573v-95.744 c0-1.965,1.607-3.573,3.572-3.573h142.903c1.965,0,3.57
document.querySelectorAll('.match-results-card').forEach((node)=>{
const hrefAttr = node.getAttribute('href');
const window = window.open(hrefAttr);
console.log(window)
})